1 2 23 package org.enhydra.tool.common; 24 25 import java.awt.BorderLayout ; 27 import java.awt.Window ; 28 import java.awt.Dialog ; 29 import java.awt.Frame ; 30 import java.awt.Point ; 31 import java.awt.event.WindowEvent ; 32 import javax.swing.JDialog ; 33 import javax.swing.JPanel ; 34 import javax.swing.JOptionPane ; 35 36 public class DialogPanel extends JPanel { 38 public final static int CANCEL_OPTION = JOptionPane.CANCEL_OPTION; 39 public final static int OK_OPTION = JOptionPane.OK_OPTION; 40 41 private int option = DialogPanel.CANCEL_OPTION; 43 private LocalDialog dialog = null; 44 private Window owner = null; 45 private boolean windowClose = false; 46 private boolean standalone = false; 47 private boolean modal = true; 48 private String title = new String (); 49 50 public Window getOwner() { 51 return owner; 52 } 53 54 public void setOwner(Window w) { 55 owner = w; 56 } 57 58 public String getTitle() { 59 return title; 60 } 61 62 public void setTitle(String t) { 63 title = t; 64 } 65 66 public boolean isModal() { 67 return modal; 68 } 69 70 public void setModal(boolean b) { 71 modal = b; 72 } 73 74 public int getOption() { 75 return option; 76 } 77 78 public void setOption(int o) { 79 option = o; 80 } 81 82 public LocalDialog getDialog() { 83 return dialog; 84 } 85 86 public void startDialogThread() { 87 Thread dialogThread = new Thread () { 88 public void run() { 89 showDialog(); 90 } 91 92 }; 93 94 dialogThread.start(); 95 } 96 97 public int showDialog() { 98 BorderLayout layout = null; 99 Point cp = null; 100 101 if (dialog == null) { 102 if (getOwner() == null) { 103 dialog = new LocalDialog(); 104 } else if (getOwner() instanceof Frame ) { 105 dialog = new LocalDialog((Frame ) getOwner()); 106 } else if (getOwner() instanceof Dialog ) { 107 dialog = new LocalDialog((Dialog ) getOwner()); 108 } else { 109 dialog = new LocalDialog(); 110 } 111 } 112 dialog.setModal(isModal()); 113 dialog.setTitle(getTitle()); 114 layout = new BorderLayout (); 115 dialog.getRootPane().setLayout(layout); 116 dialog.getRootPane().add(this, BorderLayout.CENTER); 117 dialog.pack(); 118 cp = SwingUtil.getCenteringPoint(dialog.getSize()); 119 dialog.setLocation(cp); 120 dialog.show(); 121 return option; 122 } 123 124 protected void hideDialog() { 128 if (dialog != null) { 129 dialog.setVisible(false); 130 } 131 } 132 133 protected void clearDialog() { 134 if (dialog != null) { 135 dialog.setVisible(false); 136 dialog.getRootPane().removeAll(); 137 dialog.removeAll(); 138 dialog.dispose(); 139 } 140 if (isStandalone()) { 141 System.exit(0); 142 } 143 } 144 145 protected boolean isWindowClose() { 146 return windowClose; 147 } 148 149 protected void setWindowClose(boolean b) { 150 windowClose = b; 151 } 152 153 protected boolean isStandalone() { 154 return standalone; 155 } 156 157 protected void setStandalone(boolean b) { 158 standalone = b; 159 } 160 161 private class LocalDialog extends JDialog { 162 public LocalDialog() { 163 super(); 164 } 165 166 public LocalDialog(Dialog owner) { 167 super(owner); 168 } 169 170 public LocalDialog(Frame owner) { 171 super(owner); 172 } 173 174 protected void processWindowEvent(WindowEvent e) { 175 if (isWindowClose()) { 176 if (e.getID() == WindowEvent.WINDOW_CLOSING) { 177 clearDialog(); 178 } 179 super.processWindowEvent(e); 180 } 181 } 182 183 } 184 } 185 | Popular Tags |