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