xref: /trunk/main/javainstaller2/src/JavaSetup/org/openoffice/setup/Panel/ChooseComponents.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1*9a1eeea9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9a1eeea9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9a1eeea9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9a1eeea9SAndrew Rist  * distributed with this work for additional information
6*9a1eeea9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9a1eeea9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9a1eeea9SAndrew Rist  * "License"); you may not use this file except in compliance
9*9a1eeea9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9a1eeea9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9a1eeea9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9a1eeea9SAndrew Rist  * software distributed under the License is distributed on an
15*9a1eeea9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9a1eeea9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9a1eeea9SAndrew Rist  * specific language governing permissions and limitations
18*9a1eeea9SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9a1eeea9SAndrew Rist  *************************************************************/
21*9a1eeea9SAndrew Rist 
22*9a1eeea9SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.setup.Panel;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import org.openoffice.setup.PanelHelper.PanelLabel;
27cdf0e10cSrcweir import org.openoffice.setup.PanelHelper.PanelTitle;
28cdf0e10cSrcweir import org.openoffice.setup.PanelHelper.TreeNodeRenderer;
29cdf0e10cSrcweir import org.openoffice.setup.ResourceManager;
30cdf0e10cSrcweir import org.openoffice.setup.SetupData.DisplayPackageDescription;
31cdf0e10cSrcweir import org.openoffice.setup.SetupData.SetupDataProvider;
32cdf0e10cSrcweir import java.awt.BorderLayout;
33cdf0e10cSrcweir import java.awt.ComponentOrientation;
34cdf0e10cSrcweir import java.awt.Insets;
35cdf0e10cSrcweir import java.awt.event.KeyEvent;
36cdf0e10cSrcweir import java.awt.event.KeyListener;
37cdf0e10cSrcweir import java.awt.event.MouseEvent;
38cdf0e10cSrcweir import java.awt.event.MouseListener;
39cdf0e10cSrcweir import javax.swing.BorderFactory;
40cdf0e10cSrcweir import javax.swing.JPanel;
41cdf0e10cSrcweir import javax.swing.JScrollPane;
42cdf0e10cSrcweir import javax.swing.JTree;
43cdf0e10cSrcweir import javax.swing.border.EmptyBorder;
44cdf0e10cSrcweir import javax.swing.border.TitledBorder;
45cdf0e10cSrcweir import javax.swing.event.TreeSelectionEvent;
46cdf0e10cSrcweir import javax.swing.event.TreeSelectionListener;
47cdf0e10cSrcweir import javax.swing.tree.DefaultMutableTreeNode;
48cdf0e10cSrcweir import javax.swing.tree.DefaultTreeModel;
49cdf0e10cSrcweir import javax.swing.tree.TreePath;
50cdf0e10cSrcweir import javax.swing.tree.TreeSelectionModel;
51cdf0e10cSrcweir import org.openoffice.setup.InstallData;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir public class ChooseComponents extends JPanel implements MouseListener, KeyListener, TreeSelectionListener {
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     private JTree componentTree;
56cdf0e10cSrcweir     private PanelLabel descriptionLabel;
57cdf0e10cSrcweir     private PanelLabel sizeLabel;
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     private String sizeString;
60cdf0e10cSrcweir     private PanelTitle titleBox;
61cdf0e10cSrcweir 
ChooseComponents()62cdf0e10cSrcweir     public ChooseComponents() {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         InstallData data = InstallData.getInstance();
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         setLayout(new BorderLayout());
67cdf0e10cSrcweir         setBorder(new EmptyBorder(new Insets(10, 10, 10, 10)));
68cdf0e10cSrcweir 
69cdf0e10cSrcweir         String titleText    = ResourceManager.getString("String_ChooseComponents1");
70cdf0e10cSrcweir         String subtitleText = ResourceManager.getString("String_ChooseComponents2");
71cdf0e10cSrcweir         titleBox = new PanelTitle(titleText, subtitleText, 2, 40);
72cdf0e10cSrcweir         titleBox.addVerticalStrut(20);
73cdf0e10cSrcweir         add(titleBox, BorderLayout.NORTH);
74cdf0e10cSrcweir 
75cdf0e10cSrcweir         DefaultMutableTreeNode root = SetupDataProvider.createTree();
76cdf0e10cSrcweir 
77cdf0e10cSrcweir         componentTree = new JTree(root);
78cdf0e10cSrcweir         componentTree.setShowsRootHandles(true);
79cdf0e10cSrcweir         componentTree.setRootVisible(false);
80cdf0e10cSrcweir         componentTree.setVisibleRowCount(3);
81cdf0e10cSrcweir         componentTree.setCellRenderer(new TreeNodeRenderer());
82cdf0e10cSrcweir         componentTree.addMouseListener( this );
83cdf0e10cSrcweir         componentTree.addKeyListener( this );
84cdf0e10cSrcweir         componentTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
85cdf0e10cSrcweir         componentTree.addTreeSelectionListener(this);
86cdf0e10cSrcweir         // if ( data.useRtl() ) { componentTree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir         String BorderTitle = ResourceManager.getString("String_ChooseComponents3");
89cdf0e10cSrcweir         TitledBorder PanelBorder = BorderFactory.createTitledBorder(BorderTitle);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         BorderLayout PanelLayout = new BorderLayout();
92cdf0e10cSrcweir         PanelLayout.setHgap(20);
93cdf0e10cSrcweir         JPanel DescriptionPanel = new JPanel();
94cdf0e10cSrcweir         DescriptionPanel.setBorder(PanelBorder);
95cdf0e10cSrcweir         DescriptionPanel.setLayout(PanelLayout);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         String DescriptionText = "";
98cdf0e10cSrcweir         descriptionLabel = new PanelLabel(DescriptionText, 3, 20);
99cdf0e10cSrcweir         sizeString = ResourceManager.getString("String_ChooseComponents4");
100cdf0e10cSrcweir         sizeLabel = new PanelLabel(sizeString, 1, 5);
101cdf0e10cSrcweir 
102cdf0e10cSrcweir         DescriptionPanel.add(descriptionLabel, BorderLayout.CENTER);
103cdf0e10cSrcweir         DescriptionPanel.add(sizeLabel, BorderLayout.EAST);
104cdf0e10cSrcweir         if ( data.useRtl() ) { DescriptionPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         add(new JScrollPane(componentTree), BorderLayout.CENTER);
107cdf0e10cSrcweir         add(DescriptionPanel, BorderLayout.SOUTH);
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
setTitleText(String s)110cdf0e10cSrcweir     public void setTitleText(String s) {
111cdf0e10cSrcweir         titleBox.setTitle(s);
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir 
updateNode(DefaultMutableTreeNode node)114cdf0e10cSrcweir     private void updateNode(DefaultMutableTreeNode node) {
115cdf0e10cSrcweir         if (node != null) {
116cdf0e10cSrcweir             DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject();
117cdf0e10cSrcweir             nodeInfo.toggleState(node);
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel();
120cdf0e10cSrcweir             // model.nodeChanged(node);
121cdf0e10cSrcweir 
122cdf0e10cSrcweir             // The following line was included because of task i78481.
123cdf0e10cSrcweir             // In Java 1.6 nodeChanged does not work correctly.
124cdf0e10cSrcweir             model.nodeStructureChanged(node);
125cdf0e10cSrcweir 
126cdf0e10cSrcweir             descriptionLabel.setText(nodeInfo.getDescription());
127cdf0e10cSrcweir             sizeLabel.setText(sizeString + nodeInfo.getSize());
128cdf0e10cSrcweir         }
129cdf0e10cSrcweir     }
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     /**
132cdf0e10cSrcweir      * Implement the MouseListener Interface
133cdf0e10cSrcweir      */
mouseClicked(MouseEvent event)134cdf0e10cSrcweir     public void mouseClicked(MouseEvent event)  {
135cdf0e10cSrcweir     }
mouseEntered(MouseEvent event)136cdf0e10cSrcweir     public void mouseEntered(MouseEvent event)  {
137cdf0e10cSrcweir     }
mouseExited(MouseEvent event)138cdf0e10cSrcweir     public void mouseExited(MouseEvent event)   {
139cdf0e10cSrcweir     }
mousePressed(MouseEvent event)140cdf0e10cSrcweir     public void mousePressed(MouseEvent event)  {
141cdf0e10cSrcweir         TreePath selPath = componentTree.getPathForLocation( event.getX(), event.getY() );
142cdf0e10cSrcweir         if ((selPath != null) && (componentTree.getPathBounds(selPath).getX() + 20 >= event.getX())) {
143cdf0e10cSrcweir             updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent());
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir     }
mouseReleased(MouseEvent e)146cdf0e10cSrcweir     public void mouseReleased(MouseEvent e) {
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     /**
150cdf0e10cSrcweir      * Implement the KeyListener Interface
151cdf0e10cSrcweir      */
keyPressed(KeyEvent event)152cdf0e10cSrcweir     public void keyPressed(KeyEvent event)  {
153cdf0e10cSrcweir     }
keyReleased(KeyEvent event)154cdf0e10cSrcweir     public void keyReleased(KeyEvent event) {
155cdf0e10cSrcweir     }
keyTyped(KeyEvent event)156cdf0e10cSrcweir     public void keyTyped(KeyEvent event)    {
157cdf0e10cSrcweir         if ( event.getKeyChar() == ' ' ) {
158cdf0e10cSrcweir             TreePath selPath = componentTree.getAnchorSelectionPath();
159cdf0e10cSrcweir             if ( selPath != null ) {
160cdf0e10cSrcweir                 updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent());
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir 
165cdf0e10cSrcweir     /**
166cdf0e10cSrcweir      * Implement the TreeSelectionListener Interface.
167cdf0e10cSrcweir      */
valueChanged(TreeSelectionEvent event)168cdf0e10cSrcweir     public void valueChanged(TreeSelectionEvent event) {
169cdf0e10cSrcweir         DefaultMutableTreeNode node = (DefaultMutableTreeNode)componentTree.getLastSelectedPathComponent();
170cdf0e10cSrcweir         if (node == null) {
171cdf0e10cSrcweir             descriptionLabel.setText("");
172cdf0e10cSrcweir             sizeLabel.setText("");
173cdf0e10cSrcweir         } else {
174cdf0e10cSrcweir             DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject();
175cdf0e10cSrcweir 
176cdf0e10cSrcweir             nodeInfo.updateSize(node); // important to set default values for nodes
177cdf0e10cSrcweir             DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel();
178cdf0e10cSrcweir             model.nodeChanged(node);
179cdf0e10cSrcweir 
180cdf0e10cSrcweir             descriptionLabel.setText(nodeInfo.getDescription());
181cdf0e10cSrcweir             sizeLabel.setText(sizeString + nodeInfo.getSize());
182cdf0e10cSrcweir         }
183cdf0e10cSrcweir     }
184cdf0e10cSrcweir 
185cdf0e10cSrcweir }
186