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