1 7 8 package org.enhydra.jawe.config; 9 10 11 12 13 import org.enhydra.jawe.*; 14 15 import java.util.*; 16 17 import java.awt.*; 18 import java.awt.event.*; 19 20 import javax.swing.*; 21 import javax.swing.event.*; 22 import javax.swing.border.*; 23 import javax.swing.tree.*; 24 25 import org.enhydra.jawe.*; 26 27 public class ConfigDialog extends JDialog implements ActionListener, TreeSelectionListener{ 28 29 30 private static PackageEditor parent; 31 32 private JTree paneTree; 33 private OptionTreeModel treeModel; 34 private ConfigElements jaweOptionsRoot; 35 private static ConfigDialog optionsDialog; 36 37 private String currPaneName; 38 39 private JPanel cardPanel, configsPanel; 40 private JButton okButton, cancelButton, applyButton, restoreDefaultButton; 41 42 public static ConfigDialog getInstance () { 43 if (optionsDialog==null) { 44 parent = JaWE.getInstance().getPackageEditor(); 45 optionsDialog = new ConfigDialog((JFrame)parent.getWindow()); 46 } 47 return optionsDialog; 48 } 49 50 53 public void showConfigDialog(){ 54 optionsDialog.refresh(); 55 optionsDialog.setVisible(true); 56 SwingUtilities.updateComponentTreeUI(ConfigDialog.getInstance()); 57 } 58 59 private void refresh() { 60 ArrayList cfgPanes = jaweOptionsRoot.getMembers(); 61 for (int i = 0; i < cfgPanes.size(); i++){ 62 ((BaseConfigPane) cfgPanes.get(i)).readConf(); 63 } 64 } 65 66 private ConfigDialog(Frame owner){ 67 super(owner,ResourceManager.getLanguageDependentString("ConfigTitleLabel"),true); 68 69 getContentPane().setLayout(new BorderLayout()); 70 ((JPanel)getContentPane()).setBorder( 71 BorderFactory.createEmptyBorder(5,5,5,5)); 72 73 cardPanel = new JPanel(new CardLayout()); cardPanel.setBorder(BorderFactory.createLineBorder(Color.ORANGE)); 75 getContentPane().add(cardPanel, BorderLayout.CENTER); 76 77 paneTree = new JTree(treeModel = createJaWETreeModel()); paneTree.setCellRenderer(new PaneNameRenderer()); 79 paneTree.putClientProperty("JTree.lineStyle", "Angled"); 80 paneTree.setShowsRootHandles(true); 81 getContentPane().add( 82 new JScrollPane(paneTree,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 83 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), 84 BorderLayout.WEST); 85 86 JPanel buttonsPanel = new JPanel(); 88 okButton = new JButton( 89 ResourceManager.getLanguageDependentString("OKKey"), 90 new ImageIcon(ResourceManager.getResource("OKImage"))); 91 okButton.addActionListener(this); 92 getRootPane().setDefaultButton(okButton); 93 94 cancelButton = new JButton( 95 ResourceManager.getLanguageDependentString("CancelKey"), 96 new ImageIcon(ResourceManager.getResource("CancelImage"))); 97 cancelButton.addActionListener(this); 98 99 applyButton = new JButton( 100 ResourceManager.getLanguageDependentString("ApplyKey"), 101 new ImageIcon(ResourceManager.getResource("ApplyImage"))); 102 applyButton.addActionListener(this); 103 104 restoreDefaultButton = new JButton( 105 ResourceManager.getLanguageDependentString("RestoreDefaultKey"), 106 new ImageIcon(ResourceManager.getResource("RestoreDefaultImage"))); 107 restoreDefaultButton.addActionListener(this); 108 109 buttonsPanel.add(okButton); 110 buttonsPanel.add(cancelButton); 111 buttonsPanel.add(applyButton); 112 buttonsPanel.add(Box.createHorizontalStrut(10)); 113 buttonsPanel.add(restoreDefaultButton); 114 getContentPane().add(buttonsPanel, BorderLayout.SOUTH); 115 116 addWindowListener(new WindowAdapter(){ 117 public void windowClosing(WindowEvent e){ 118 cancel(); 119 } 120 }); 121 122 TreePath jawePath = new TreePath(new Object [] { treeModel.getRoot(), 123 jaweOptionsRoot.getMember(0) }); 124 paneTree.setSelectionPath(jawePath); 125 126 paneTree.getSelectionModel().addTreeSelectionListener(this); 127 128 getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) 129 .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0,false),"Cancel"); 130 getRootPane().getActionMap().put("Cancel", new AbstractAction() { 131 public void actionPerformed(ActionEvent e) { 132 cancel(); 133 } 134 }); 135 136 Utils.center(this,100,250); 137 138 } 139 140 private OptionTreeModel createJaWETreeModel(){ 141 142 jaweOptionsRoot = new ConfigElements("JaWEConfiguration"); 143 144 OptionTreeModel jaweTreeModel = new OptionTreeModel(jaweOptionsRoot); 145 146 addOptionPane(new CfgGeneral("General"), jaweOptionsRoot); 147 addOptionPane(new CfgUI("UI"), jaweOptionsRoot); 148 addOptionPane(new CfgLDAP("LDAP"), jaweOptionsRoot); 149 addOptionPane(new CfgColors("Color"), jaweOptionsRoot); 150 addOptionPane(new CfgObjectSize("Size"), jaweOptionsRoot); 151 152 return jaweTreeModel; 153 } 154 155 private void addOptionPane(BaseConfigPane child, ConfigElements parent){ 156 String name = child.getName(); 157 160 configsPanel = new JPanel(new BorderLayout()); 161 configsPanel.add(child.getComponent(),BorderLayout.NORTH); 162 cardPanel.add(new JScrollPane(configsPanel, 163 JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, 164 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED),name); 165 166 parent.addOptionPane(child); 167 } 168 169 private void cancel(){ 170 optionsDialog.setVisible(false); 171 } 172 173 private void ok(boolean close){ 174 OptionTreeModel m = (OptionTreeModel) paneTree.getModel(); 175 ((ConfigElements) m.getRoot()).save(); 176 applyChanges(); 177 178 if(close){ 180 optionsDialog.setVisible(false); 181 } 182 } 183 184 private void applyChanges () { 185 OptionTreeModel m = (OptionTreeModel) paneTree.getModel(); 186 ((ConfigElements) m.getRoot()).readConf(); 187 188 PackageEditor pe=JaWE.getInstance().getPackageEditor(); 190 pe.refreshEditorConfiguration(); 191 SwingUtilities.updateComponentTreeUI(ConfigDialog.getInstance()); 192 JaWE.getInstance().getPackageEditor().getPackageTreePanel().refreshPackageTreePanel(); 194 JaWEConfig.getInstance().saveConf(); 195 } 196 197 200 public void actionPerformed(ActionEvent e) { 201 Object source = e.getSource(); 202 if (source == okButton){ 203 ok(true); 204 }else if (source == cancelButton){ 205 cancel(); 206 }else if (source == applyButton){ 207 ok(false); 208 }else if (source == restoreDefaultButton){ 209 JaWEConfig.getInstance().restoreDefaultSettings(); 210 applyChanges(); 211 } 212 } 213 214 218 public void valueChanged(TreeSelectionEvent evt) { 219 TreePath path = evt.getPath(); 220 221 if (path == null || !(path.getLastPathComponent() instanceof BaseConfigPane)) 222 return; 223 224 Object [] nodes = path.getPath(); 225 int lastIdx = nodes.length - 1; 226 currPaneName = null; 227 228 for (int i = paneTree.isRootVisible() ? 0 : 1; i <= lastIdx; i++){ 229 if (nodes[i] instanceof BaseConfigPane){ 230 currPaneName = ((BaseConfigPane)nodes[i]).getName(); 231 } else if (nodes[i] instanceof ConfigElements) { 232 currPaneName = ((ConfigElements)nodes[i]).getName(); 233 } else { 234 continue; 235 } 236 237 } 238 ((CardLayout) cardPanel.getLayout()).show(cardPanel, currPaneName); 239 } 240 241 public void refreshLanguageDependentStrings () { 242 setTitle(ResourceManager.getLanguageDependentString("ConfigTitleLabel")); 243 okButton.setText(ResourceManager.getLanguageDependentString("OKKey")); 244 cancelButton.setText(ResourceManager.getLanguageDependentString("CancelKey")); 245 applyButton.setText(ResourceManager.getLanguageDependentString("ApplyKey")); 246 restoreDefaultButton.setText(ResourceManager.getLanguageDependentString("RestoreDefaultKey")); 247 OptionTreeModel m = (OptionTreeModel) paneTree.getModel(); 248 ((ConfigElements) m.getRoot()).refreshLanguageDependentStrings(); 249 } 250 251 class PaneNameRenderer extends JLabel implements TreeCellRenderer{ 252 private Border noFocusBorder = BorderFactory.createEmptyBorder(1, 1, 1, 1); 253 private Border focusBorder = BorderFactory.createLineBorder(Color.ORANGE); 254 255 private Font paneFont; 256 private Font rootFont; 257 258 public PaneNameRenderer(){ 259 setOpaque(true); 260 261 paneFont = UIManager.getFont("Tree.font"); 262 rootFont = new Font(paneFont.getName(), 263 paneFont.getStyle() | Font.BOLD, 264 paneFont.getSize()+1); 265 } 266 267 public Component getTreeCellRendererComponent(JTree tree, Object value, 268 boolean selected, 269 boolean expanded, 270 boolean leaf, int row, 271 boolean hasFocus){ 272 273 if (selected){ 274 this.setBackground(UIManager.getColor("Tree.selectionBackground")); 275 this.setForeground(UIManager.getColor("Tree.selectionForeground")); 276 } else { 277 this.setBackground(tree.getBackground()); 278 this.setForeground(tree.getForeground()); 279 } 280 281 String name = null; 282 283 if (value instanceof ConfigElements){ 284 name = ((ConfigElements) value).getName(); 285 this.setFont(rootFont); 286 } else if (value instanceof BaseConfigPane) { 287 name = ((BaseConfigPane) value).getName(); 288 this.setFont(paneFont); 289 } 290 291 if (name == null){ 292 setText(null); 293 } else { 294 String label = ResourceManager.getLanguageDependentString("Config."+ name + 295 ".Label"); 296 297 if (label == null){ 298 setText(name); 299 } else { 300 setText(label); 301 } 302 } 303 304 setBorder(hasFocus ? focusBorder : noFocusBorder); 305 return this; 306 } 307 } 308 309 class OptionTreeModel implements TreeModel{ 310 311 private ConfigElements root; private EventListenerList listenerList = new EventListenerList(); 313 314 public OptionTreeModel(ConfigElements root){ 315 this.root=root; 316 } 317 318 327 public void valueForPathChanged(TreePath path, Object newValue) { 328 } 330 331 337 public Object getRoot() { 338 return root; 339 } 340 341 351 public int getIndexOfChild(Object parent, Object child) { 352 if (parent instanceof ConfigElements){ 353 return ((ConfigElements) parent).getMemberIndex(child); 354 } else { 355 return -1; 356 } 357 } 358 359 371 public Object getChild(Object parent, int index) { 372 if (parent instanceof ConfigElements){ 373 return ((ConfigElements)parent).getMember(index); 374 } else { 375 return null; 376 } 377 } 378 379 386 public void removeTreeModelListener(TreeModelListener l) { 387 listenerList.remove(TreeModelListener.class, l); 388 } 389 390 399 public int getChildCount(Object parent) { 400 if (parent instanceof ConfigElements){ 401 return ((ConfigElements)parent).getMemberCount(); 402 } else { 403 return 0; 404 } 405 } 406 407 418 public boolean isLeaf(Object node) { 419 return node instanceof BaseConfigPane; 420 } 421 422 429 public void addTreeModelListener(TreeModelListener l) { 430 listenerList.add(TreeModelListener.class, l); 431 } 432 } 433 434 435 } 436 437 438 | Popular Tags |