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
10*9a1eeea9SAndrew Rist  *
11*9a1eeea9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9a1eeea9SAndrew Rist  *
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.
19*9a1eeea9SAndrew Rist  *
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 
54cdf0e10cSrcweir public class ChooseUninstallationComponents extends JPanel implements MouseListener, KeyListener, TreeSelectionListener {
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     private JTree componentTree;
57cdf0e10cSrcweir     private PanelLabel descriptionLabel;
58cdf0e10cSrcweir     private PanelLabel sizeLabel;
59cdf0e10cSrcweir 
60cdf0e10cSrcweir     private String sizeString;
61cdf0e10cSrcweir 
ChooseUninstallationComponents()62cdf0e10cSrcweir     public ChooseUninstallationComponents() {
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_ChooseUninstallationComponents2");
71cdf0e10cSrcweir         PanelTitle titleBox = new PanelTitle(titleText, subtitleText, 2, 40);
72cdf0e10cSrcweir         // PanelTitle titleBox = new PanelTitle(titleText, subtitleText);
73cdf0e10cSrcweir         titleBox.addVerticalStrut(20);
74cdf0e10cSrcweir         add(titleBox, BorderLayout.NORTH);
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         DefaultMutableTreeNode root = SetupDataProvider.createTree();
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         componentTree = new JTree(root);
79cdf0e10cSrcweir         componentTree.setShowsRootHandles(true);
80cdf0e10cSrcweir         componentTree.setRootVisible(false);
81cdf0e10cSrcweir         componentTree.setVisibleRowCount(3);
82cdf0e10cSrcweir         componentTree.setCellRenderer(new TreeNodeRenderer());
83cdf0e10cSrcweir         componentTree.addMouseListener( this );
84cdf0e10cSrcweir         componentTree.addKeyListener( this );
85cdf0e10cSrcweir         componentTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
86cdf0e10cSrcweir         componentTree.addTreeSelectionListener(this);
87cdf0e10cSrcweir         // if ( data.useRtl() ) { componentTree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         String BorderTitle = ResourceManager.getString("String_ChooseComponents3");
90cdf0e10cSrcweir         TitledBorder PanelBorder = BorderFactory.createTitledBorder(BorderTitle);
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         BorderLayout PanelLayout = new BorderLayout();
93cdf0e10cSrcweir         PanelLayout.setHgap(20);
94cdf0e10cSrcweir         JPanel DescriptionPanel = new JPanel();
95cdf0e10cSrcweir         DescriptionPanel.setBorder(PanelBorder);
96cdf0e10cSrcweir         DescriptionPanel.setLayout(PanelLayout);
97cdf0e10cSrcweir 
98cdf0e10cSrcweir         String DescriptionText = "";
99cdf0e10cSrcweir         descriptionLabel = new PanelLabel(DescriptionText, 3, 20);
100cdf0e10cSrcweir         sizeString = ResourceManager.getString("String_ChooseComponents4");
101cdf0e10cSrcweir         sizeLabel = new PanelLabel(sizeString, 1, 5);
102cdf0e10cSrcweir         if ( data.useRtl() ) { descriptionLabel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); }
103cdf0e10cSrcweir 
104cdf0e10cSrcweir         DescriptionPanel.add(descriptionLabel, BorderLayout.CENTER);
105cdf0e10cSrcweir         DescriptionPanel.add(sizeLabel, BorderLayout.EAST);
106cdf0e10cSrcweir         if ( data.useRtl() ) { DescriptionPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         add(new JScrollPane(componentTree), BorderLayout.CENTER);
109cdf0e10cSrcweir         add(DescriptionPanel, BorderLayout.SOUTH);
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir 
updateNode(DefaultMutableTreeNode node)112cdf0e10cSrcweir     private void updateNode(DefaultMutableTreeNode node) {
113cdf0e10cSrcweir         if (node != null) {
114cdf0e10cSrcweir             DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject();
115cdf0e10cSrcweir             nodeInfo.toggleState(node);
116cdf0e10cSrcweir 
117cdf0e10cSrcweir             DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel();
118cdf0e10cSrcweir             model.nodeChanged(node);
119cdf0e10cSrcweir 
120cdf0e10cSrcweir             descriptionLabel.setText(nodeInfo.getDescription());
121cdf0e10cSrcweir             sizeLabel.setText(sizeString + nodeInfo.getSize());
122cdf0e10cSrcweir         }
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir     /**
126cdf0e10cSrcweir      * Implement the MouseListener Interface
127cdf0e10cSrcweir      */
mouseClicked(MouseEvent event)128cdf0e10cSrcweir     public void mouseClicked(MouseEvent event)  {
129cdf0e10cSrcweir     }
mouseEntered(MouseEvent event)130cdf0e10cSrcweir     public void mouseEntered(MouseEvent event)  {
131cdf0e10cSrcweir     }
mouseExited(MouseEvent event)132cdf0e10cSrcweir     public void mouseExited(MouseEvent event)   {
133cdf0e10cSrcweir     }
mousePressed(MouseEvent event)134cdf0e10cSrcweir     public void mousePressed(MouseEvent event)  {
135cdf0e10cSrcweir         TreePath selPath = componentTree.getPathForLocation( event.getX(), event.getY() );
136cdf0e10cSrcweir         if ((selPath != null) && (componentTree.getPathBounds(selPath).getX() + 20 >= event.getX())) {
137cdf0e10cSrcweir             updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent());
138cdf0e10cSrcweir         }
139cdf0e10cSrcweir     }
mouseReleased(MouseEvent e)140cdf0e10cSrcweir     public void mouseReleased(MouseEvent e) {
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     /**
144cdf0e10cSrcweir      * Implement the KeyListener Interface
145cdf0e10cSrcweir      */
keyPressed(KeyEvent event)146cdf0e10cSrcweir     public void keyPressed(KeyEvent event)  {
147cdf0e10cSrcweir     }
keyReleased(KeyEvent event)148cdf0e10cSrcweir     public void keyReleased(KeyEvent event) {
149cdf0e10cSrcweir     }
keyTyped(KeyEvent event)150cdf0e10cSrcweir     public void keyTyped(KeyEvent event)    {
151cdf0e10cSrcweir         if ( event.getKeyChar() == ' ' ) {
152cdf0e10cSrcweir             TreePath selPath = componentTree.getAnchorSelectionPath();
153cdf0e10cSrcweir             if ( selPath != null ) {
154cdf0e10cSrcweir                 updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent());
155cdf0e10cSrcweir             }
156cdf0e10cSrcweir         }
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     /**
160cdf0e10cSrcweir      * Implement the TreeSelectionListener Interface.
161cdf0e10cSrcweir      */
valueChanged(TreeSelectionEvent event)162cdf0e10cSrcweir     public void valueChanged(TreeSelectionEvent event) {
163cdf0e10cSrcweir         DefaultMutableTreeNode node = (DefaultMutableTreeNode)componentTree.getLastSelectedPathComponent();
164cdf0e10cSrcweir         if (node == null) {
165cdf0e10cSrcweir             descriptionLabel.setText("");
166cdf0e10cSrcweir             sizeLabel.setText("");
167cdf0e10cSrcweir         } else {
168cdf0e10cSrcweir             DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject();
169cdf0e10cSrcweir 
170cdf0e10cSrcweir             nodeInfo.updateSize(node); // important to set default values for nodes
171cdf0e10cSrcweir             DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel();
172cdf0e10cSrcweir             model.nodeChanged(node);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir             descriptionLabel.setText(nodeInfo.getDescription());
175cdf0e10cSrcweir             sizeLabel.setText(sizeString + nodeInfo.getSize());
176cdf0e10cSrcweir         }
177cdf0e10cSrcweir     }
178cdf0e10cSrcweir }
179