1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package org.openoffice.setup.Panel; 29 30 import org.openoffice.setup.PanelHelper.PanelLabel; 31 import org.openoffice.setup.PanelHelper.PanelTitle; 32 import org.openoffice.setup.PanelHelper.TreeNodeRenderer; 33 import org.openoffice.setup.ResourceManager; 34 import org.openoffice.setup.SetupData.DisplayPackageDescription; 35 import org.openoffice.setup.SetupData.SetupDataProvider; 36 import java.awt.BorderLayout; 37 import java.awt.ComponentOrientation; 38 import java.awt.Insets; 39 import java.awt.event.KeyEvent; 40 import java.awt.event.KeyListener; 41 import java.awt.event.MouseEvent; 42 import java.awt.event.MouseListener; 43 import javax.swing.BorderFactory; 44 import javax.swing.JPanel; 45 import javax.swing.JScrollPane; 46 import javax.swing.JTree; 47 import javax.swing.border.EmptyBorder; 48 import javax.swing.border.TitledBorder; 49 import javax.swing.event.TreeSelectionEvent; 50 import javax.swing.event.TreeSelectionListener; 51 import javax.swing.tree.DefaultMutableTreeNode; 52 import javax.swing.tree.DefaultTreeModel; 53 import javax.swing.tree.TreePath; 54 import javax.swing.tree.TreeSelectionModel; 55 import org.openoffice.setup.InstallData; 56 57 public class ChooseComponents extends JPanel implements MouseListener, KeyListener, TreeSelectionListener { 58 59 private JTree componentTree; 60 private PanelLabel descriptionLabel; 61 private PanelLabel sizeLabel; 62 63 private String sizeString; 64 private PanelTitle titleBox; 65 66 public ChooseComponents() { 67 68 InstallData data = InstallData.getInstance(); 69 70 setLayout(new BorderLayout()); 71 setBorder(new EmptyBorder(new Insets(10, 10, 10, 10))); 72 73 String titleText = ResourceManager.getString("String_ChooseComponents1"); 74 String subtitleText = ResourceManager.getString("String_ChooseComponents2"); 75 titleBox = new PanelTitle(titleText, subtitleText, 2, 40); 76 titleBox.addVerticalStrut(20); 77 add(titleBox, BorderLayout.NORTH); 78 79 DefaultMutableTreeNode root = SetupDataProvider.createTree(); 80 81 componentTree = new JTree(root); 82 componentTree.setShowsRootHandles(true); 83 componentTree.setRootVisible(false); 84 componentTree.setVisibleRowCount(3); 85 componentTree.setCellRenderer(new TreeNodeRenderer()); 86 componentTree.addMouseListener( this ); 87 componentTree.addKeyListener( this ); 88 componentTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 89 componentTree.addTreeSelectionListener(this); 90 // if ( data.useRtl() ) { componentTree.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } 91 92 String BorderTitle = ResourceManager.getString("String_ChooseComponents3"); 93 TitledBorder PanelBorder = BorderFactory.createTitledBorder(BorderTitle); 94 95 BorderLayout PanelLayout = new BorderLayout(); 96 PanelLayout.setHgap(20); 97 JPanel DescriptionPanel = new JPanel(); 98 DescriptionPanel.setBorder(PanelBorder); 99 DescriptionPanel.setLayout(PanelLayout); 100 101 String DescriptionText = ""; 102 descriptionLabel = new PanelLabel(DescriptionText, 3, 20); 103 sizeString = ResourceManager.getString("String_ChooseComponents4"); 104 sizeLabel = new PanelLabel(sizeString, 1, 5); 105 106 DescriptionPanel.add(descriptionLabel, BorderLayout.CENTER); 107 DescriptionPanel.add(sizeLabel, BorderLayout.EAST); 108 if ( data.useRtl() ) { DescriptionPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } 109 110 add(new JScrollPane(componentTree), BorderLayout.CENTER); 111 add(DescriptionPanel, BorderLayout.SOUTH); 112 } 113 114 public void setTitleText(String s) { 115 titleBox.setTitle(s); 116 } 117 118 private void updateNode(DefaultMutableTreeNode node) { 119 if (node != null) { 120 DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject(); 121 nodeInfo.toggleState(node); 122 123 DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel(); 124 // model.nodeChanged(node); 125 126 // The following line was included because of task i78481. 127 // In Java 1.6 nodeChanged does not work correctly. 128 model.nodeStructureChanged(node); 129 130 descriptionLabel.setText(nodeInfo.getDescription()); 131 sizeLabel.setText(sizeString + nodeInfo.getSize()); 132 } 133 } 134 135 /** 136 * Implement the MouseListener Interface 137 */ 138 public void mouseClicked(MouseEvent event) { 139 } 140 public void mouseEntered(MouseEvent event) { 141 } 142 public void mouseExited(MouseEvent event) { 143 } 144 public void mousePressed(MouseEvent event) { 145 TreePath selPath = componentTree.getPathForLocation( event.getX(), event.getY() ); 146 if ((selPath != null) && (componentTree.getPathBounds(selPath).getX() + 20 >= event.getX())) { 147 updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent()); 148 } 149 } 150 public void mouseReleased(MouseEvent e) { 151 } 152 153 /** 154 * Implement the KeyListener Interface 155 */ 156 public void keyPressed(KeyEvent event) { 157 } 158 public void keyReleased(KeyEvent event) { 159 } 160 public void keyTyped(KeyEvent event) { 161 if ( event.getKeyChar() == ' ' ) { 162 TreePath selPath = componentTree.getAnchorSelectionPath(); 163 if ( selPath != null ) { 164 updateNode((DefaultMutableTreeNode)selPath.getLastPathComponent()); 165 } 166 } 167 } 168 169 /** 170 * Implement the TreeSelectionListener Interface. 171 */ 172 public void valueChanged(TreeSelectionEvent event) { 173 DefaultMutableTreeNode node = (DefaultMutableTreeNode)componentTree.getLastSelectedPathComponent(); 174 if (node == null) { 175 descriptionLabel.setText(""); 176 sizeLabel.setText(""); 177 } else { 178 DisplayPackageDescription nodeInfo = (DisplayPackageDescription)node.getUserObject(); 179 180 nodeInfo.updateSize(node); // important to set default values for nodes 181 DefaultTreeModel model = (DefaultTreeModel)componentTree.getModel(); 182 model.nodeChanged(node); 183 184 descriptionLabel.setText(nodeInfo.getDescription()); 185 sizeLabel.setText(sizeString + nodeInfo.getSize()); 186 } 187 } 188 189 } 190