1 2 24 package org.enhydra.tool.common; 25 26 import org.enhydra.tool.ToolBoxInfo; 28 import org.enhydra.tool.common.AboutBox; 29 import org.enhydra.tool.common.ButtonPanel; 30 import org.enhydra.tool.common.InnerPanel; 31 import org.enhydra.tool.common.ProgressMeter; 32 import org.enhydra.tool.common.SwingUtil; 33 import org.enhydra.tool.common.event.HelpEvent; 34 import org.enhydra.tool.common.event.HelpListener; 35 36 import java.awt.event.ActionEvent ; 38 import java.awt.event.ActionListener ; 39 import java.util.ArrayList ; 40 import java.util.Arrays ; 41 import java.util.ResourceBundle ; 42 import java.awt.Component ; 43 import java.awt.Container ; 44 import java.awt.Dialog ; 45 import java.awt.Frame ; 46 import java.awt.GridBagLayout ; 47 import java.awt.GridBagConstraints ; 48 import java.awt.Insets ; 49 import java.awt.Point ; 50 import java.awt.Window ; 51 import javax.swing.JComponent ; 52 import javax.swing.JOptionPane ; 53 import javax.swing.JPanel ; 54 import javax.swing.JDialog ; 55 56 abstract public class DialogHandler { 58 59 public static ResourceBundle res = 61 ResourceBundle.getBundle("org.enhydra.tool.common.Res"); 63 private boolean standalone = false; 65 private int option = JOptionPane.CANCEL_OPTION; 66 private ProgressMeter progress = null; 67 private HelpListener[] helpListeners = new HelpListener[0]; 68 69 public DialogHandler() { 70 pmInit(); 71 } 72 73 abstract public InnerPanel getInnerPanel(); 74 abstract public ButtonPanel getButtonPanel(); 75 abstract public ActionListener createButtonListener(); 76 abstract public String getTitle(); 77 78 public int getOption() { 79 return option; 80 } 81 82 public void setOption(int op) { 83 option = op; 84 } 85 86 public synchronized void addHelpListener(HelpListener l) { 88 ArrayList list = null; 89 90 list = new ArrayList (Arrays.asList(helpListeners)); 91 if (!list.contains(l)) { 92 list.add(l); 93 list.trimToSize(); 94 helpListeners = new HelpListener[list.size()]; 95 helpListeners = (HelpListener[]) list.toArray(helpListeners); 96 } 97 list.clear(); 98 } 99 100 public synchronized void removeHelpListener(HelpListener l) { 101 ArrayList list = null; 102 103 list = new ArrayList (Arrays.asList(helpListeners)); 104 if (list.contains(l)) { 105 list.remove(l); 106 list.trimToSize(); 107 helpListeners = new HelpListener[list.size()]; 108 helpListeners = (HelpListener[]) list.toArray(helpListeners); 109 } 110 list.clear(); 111 } 112 113 public void notifyHelpListeners(Object source) { 114 HelpEvent event = null; 115 116 if (source instanceof JComponent ) { 117 JComponent comp = (JComponent ) source; 118 119 event = new HelpEvent(comp.getTopLevelAncestor()); 120 } else { 121 event = new HelpEvent(source); 122 } 123 for (int i = 0; i < helpListeners.length; i++) { 124 helpListeners[i].onHelp(event); 125 } 126 } 127 128 129 130 public void showAbout() { 131 StringBuffer textBuf = new StringBuffer (); 132 StringBuffer titleBuf = new StringBuffer (); 133 AboutBox about = null; 134 Container ancestor = null; 135 136 ancestor = getInnerPanel().getTopLevelAncestor(); 137 textBuf.append("Enhydra Application Server 6.5"); 138 textBuf.append('\n'); 141 textBuf.append(Constants.XMLC); 142 textBuf.append(' '); 143 textBuf.append(ToolBoxInfo.getXMLCVersion()); 144 if (ancestor instanceof Frame ) { 145 about = new AboutBox((Frame ) ancestor); 146 } else if (ancestor instanceof Dialog ) { 147 about = new AboutBox((Dialog ) ancestor); 148 } else { 149 about = new AboutBox(new Frame ()); 150 } 151 titleBuf.append(Constants.KELP); 152 titleBuf.append(' '); 153 titleBuf.append(ToolBoxInfo.getToolBoxVersion()); 154 about.setTitleText(titleBuf.toString()); 155 about.setMediumText(textBuf.toString()); 156 about.show(); 158 about.removeAll(); 159 about.dispose(); 160 } 161 162 public int showDialog(Component parent) { 163 JPanel panelBottom = null; 164 JDialog dialog = null; 165 Point cPoint; 166 167 getInnerPanel().initPreferredSize(); 168 if (parent == null) { 169 dialog = new JDialog (); 170 } else if (parent instanceof Frame ) { 171 dialog = new JDialog ((Frame ) parent); 172 } else if (parent instanceof Dialog ) { 173 dialog = new JDialog ((Dialog ) parent); 174 } else { 175 dialog = new JDialog (); 176 } 177 getButtonPanel().addActionListener(createButtonListener()); 178 if (helpListeners.length == 0) { 179 getButtonPanel().removeHelp(); 180 } 181 panelBottom = new JPanel (); 182 panelBottom.setLayout(new GridBagLayout ()); 183 dialog.setTitle(getTitle()); 184 dialog.setModal(true); 185 dialog.getContentPane().setLayout(new GridBagLayout ()); 186 dialog.getContentPane().add(getInnerPanel(), 187 new GridBagConstraints (0, 0, 1, 1, 0.75, 188 0.75, GridBagConstraints.CENTER, 189 GridBagConstraints.BOTH, 190 new Insets (0, 0, 0, 0), 0, 0)); 191 dialog.getContentPane().add(getButtonPanel(), 192 new GridBagConstraints (0, 1, 1, 1, 0.25, 193 0.25, GridBagConstraints.CENTER, 194 GridBagConstraints.BOTH, 195 new Insets (0, 0, 0, 0), 0, 0)); 196 dialog.invalidate(); 197 dialog.doLayout(); 198 dialog.pack(); 199 cPoint = SwingUtil.getCenteringPoint(dialog.getSize()); 200 dialog.setLocation(cPoint); 201 getInnerPanel().preShow(); 202 dialog.show(); 203 return getOption(); 204 } 205 206 210 public void closeWindow() { 211 Window window = null; 212 213 try { 214 getInnerPanel().save(); 215 } catch (Exception e) { 216 e.printStackTrace(System.err); 217 } 218 if (getInnerPanel().getTopLevelAncestor() instanceof Window ) { 219 window = (Window ) getInnerPanel().getTopLevelAncestor(); 220 } 221 if (window != null) { 222 window.removeAll(); 223 window.dispose(); 224 clearAll(); 225 } 226 if (standalone) { 227 System.exit(0); 228 } 229 } 230 231 234 protected void clearAll() { 235 getInnerPanel().clearAll(); 236 getButtonPanel().clearAll(); 237 helpListeners = new HelpListener[0]; 238 } 239 240 public ProgressMeter getProgressMeter() { 241 return progress; 242 } 243 244 public void openProgress() { 245 progress.setModal(true); 246 progress.setTitle(getProgressTitle()); 247 progress.setOwner((Window ) getInnerPanel().getTopLevelAncestor()); 248 progress.startDialogThread(); 249 } 250 251 abstract protected String getProgressTitle(); 255 256 private void pmInit() { 260 progress = new ProgressMeter(); 261 progress.setDisposeOnClose(false); 262 progress.setTitle(getProgressTitle()); 263 } 264 265 } 266 | Popular Tags |