1 19 20 package org.gjt.sp.jedit.gui; 21 22 import javax.swing.*; 23 import java.awt.event.*; 24 import java.awt.*; 25 26 34 public abstract class EnhancedDialog extends JDialog 35 { 36 public EnhancedDialog(Frame parent, String title, boolean modal) 37 { 38 super(parent,title,modal); 39 _init(); 40 } 41 42 public EnhancedDialog(Dialog parent, String title, boolean modal) 43 { 44 super(parent,title,modal); 45 _init(); 46 } 47 48 public boolean getEnterEnabled() 49 { 50 return enterEnabled; 51 } 52 53 public void setEnterEnabled(boolean enterEnabled) 54 { 55 this.enterEnabled = enterEnabled; 56 } 57 58 public abstract void ok(); 59 public abstract void cancel(); 60 61 private void _init() 63 { 64 ((Container)getLayeredPane()).addContainerListener( 65 new ContainerHandler()); 66 getContentPane().addContainerListener(new ContainerHandler()); 67 68 keyHandler = new KeyHandler(); 69 addKeyListener(keyHandler); 70 addWindowListener(new WindowHandler()); 71 72 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 73 74 enterEnabled = true; 75 } 76 78 protected KeyHandler keyHandler; 80 protected boolean enterEnabled; 81 82 class ContainerHandler extends ContainerAdapter 84 { 85 public void componentAdded(ContainerEvent evt) 86 { 87 componentAdded(evt.getChild()); 88 } 89 90 public void componentRemoved(ContainerEvent evt) 91 { 92 componentRemoved(evt.getChild()); 93 } 94 95 private void componentAdded(Component comp) 96 { 97 comp.addKeyListener(keyHandler); 98 if(comp instanceof Container) 99 { 100 Container cont = (Container)comp; 101 cont.addContainerListener(this); 102 Component[] comps = cont.getComponents(); 103 for(int i = 0; i < comps.length; i++) 104 { 105 componentAdded(comps[i]); 106 } 107 } 108 } 109 110 private void componentRemoved(Component comp) 111 { 112 comp.removeKeyListener(keyHandler); 113 if(comp instanceof Container) 114 { 115 Container cont = (Container)comp; 116 cont.removeContainerListener(this); 117 Component[] comps = cont.getComponents(); 118 for(int i = 0; i < comps.length; i++) 119 { 120 componentRemoved(comps[i]); 121 } 122 } 123 } 124 } 125 126 class KeyHandler extends KeyAdapter 127 { 128 public void keyPressed(KeyEvent evt) 129 { 130 if(evt.isConsumed()) 131 return; 132 133 if(evt.getKeyCode() == KeyEvent.VK_ENTER 134 && enterEnabled) 135 { 136 Component comp = getFocusOwner(); 137 while(comp != null) 138 { 139 if(comp instanceof JComboBox) 140 { 141 JComboBox combo = (JComboBox)comp; 142 if(combo.isEditable()) 143 { 144 Object selected = combo.getEditor().getItem(); 145 if(selected != null) 146 combo.setSelectedItem(selected); 147 } 148 break; 149 } 150 151 comp = comp.getParent(); 152 } 153 154 ok(); 155 evt.consume(); 156 } 157 else if(evt.getKeyCode() == KeyEvent.VK_ESCAPE) 158 { 159 cancel(); 160 evt.consume(); 161 } 162 } 163 } 164 165 class WindowHandler extends WindowAdapter 166 { 167 public void windowClosing(WindowEvent evt) 168 { 169 cancel(); 170 } 171 } 172 } 173 | Popular Tags |