1 2 24 package org.enhydra.tool.common; 25 26 import org.enhydra.tool.common.event.FirstFocusEvent; 28 import org.enhydra.tool.common.event.FirstFocusListener; 29 30 import java.awt.*; 32 import java.awt.event.FocusAdapter ; 33 import java.awt.event.FocusEvent ; 34 import java.awt.event.WindowEvent ; 35 import java.awt.event.WindowAdapter ; 36 import java.beans.*; 37 import java.lang.ref.WeakReference ; 38 import java.util.ArrayList ; 39 import java.util.Arrays ; 40 import javax.swing.*; 41 42 public class InnerPanel extends JPanel implements FirstFocusListener { 44 private boolean init = true; 45 private boolean saveOnClose = false; 46 private String progressTitle = new String (); 47 private FirstFocusListener[] firstListeners = new FirstFocusListener[0]; 48 private LocalFocusListener focusListener = null; 49 private LocalWindowListener windowListener = null; 50 51 public static InnerPanel findAncestor(Component comp) { 52 Container ancestor = null; 53 InnerPanel found = null; 54 55 ancestor = comp.getParent(); 56 while (ancestor != null) { 57 ancestor = ancestor.getParent(); 58 if (ancestor instanceof InnerPanel) { 59 found = (InnerPanel) ancestor; 60 break; 61 } 62 } 63 return found; 64 } 65 66 public InnerPanel() { 67 super(); 68 } 69 70 public Window getWindow() { 71 Window window = null; 72 73 if (getTopLevelAncestor() instanceof Window) { 74 window = (Window) getTopLevelAncestor(); 75 } 76 return window; 77 } 78 79 public Dialog getDialog() { 80 Dialog dialog = null; 81 82 if (getWindow() instanceof Dialog) { 83 dialog = (Dialog) getWindow(); 84 } 85 return dialog; 86 } 87 88 public void setSaveOnClose(boolean b) { 89 saveOnClose = b; 90 if (saveOnClose) { 91 preShow(); 92 } 93 } 94 95 public boolean isSaveOnClose() { 96 return saveOnClose; 97 } 98 99 102 public void clearAll() { 103 clearWindowListener(); 104 clearFocusListener(); 105 removeAll(); 106 firstListeners = new FirstFocusListener[0]; 107 } 108 109 public String getProgressTitle() { 110 return progressTitle; 111 } 112 113 public void setProgressTitle(String t) { 114 progressTitle = t; 115 } 116 117 public void initPreferredSize() { 118 Dimension d = new Dimension(); 119 120 d.height = 342; 121 d.width = 542; 122 setPreferredSize(d); 123 } 124 125 public void save() { 126 System.err.println("InnerPanel.save() not implemented"); 127 } 128 129 public void addFirstFocusListener(FirstFocusListener listener) { 130 ArrayList list = null; 131 132 list = new ArrayList (Arrays.asList(firstListeners)); 133 if (!list.contains(listener)) { 134 list.add(listener); 135 list.trimToSize(); 136 firstListeners = new FirstFocusListener[list.size()]; 137 firstListeners = 138 (FirstFocusListener[]) list.toArray(firstListeners); 139 } 140 list.clear(); 141 } 142 143 public void removeFirstFocusListener(FirstFocusListener listener) { 144 ArrayList list = null; 145 146 list = new ArrayList (Arrays.asList(firstListeners)); 147 if (list.contains(listener)) { 148 list.remove(listener); 149 list.trimToSize(); 150 firstListeners = new FirstFocusListener[list.size()]; 151 firstListeners = 152 (FirstFocusListener[]) list.toArray(firstListeners); 153 } 154 list.clear(); 155 } 156 157 private void notifyFirstFocusListeners() { 158 FirstFocusEvent event = new FirstFocusEvent(this); 159 160 for (int i = 0; i < firstListeners.length; i++) { 161 firstListeners[i].onFirstFocus(event); 162 } 163 firstListeners = new FirstFocusListener[0]; 164 } 165 166 protected Component[] getFirstFocusComponents() { 168 Component[] comps = new Component[1]; 169 170 comps[0] = this; 171 System.err.println("InnerPanel.getFirstFocusComponents() not implemented"); 172 return comps; 173 } 174 175 public void onFirstFocus(FirstFocusEvent event) { 176 preShow(); 177 if (isSaveOnClose()) { 178 initWindowListener(); 179 } 180 clearFocusListener(); 181 } 182 183 public void preShow() { 184 Component[] comps = getFirstFocusComponents(); 185 addFirstFocusListener(this); 186 if (focusListener == null) { 187 focusListener = new LocalFocusListener(); 188 for (int i = 0; i < comps.length; i++) { 189 comps[i].addFocusListener(focusListener); 190 } 191 } 192 } 193 194 private void clearFocusListener() { 195 Component[] comps = getFirstFocusComponents(); 196 197 if (focusListener != null) { 198 for (int i = 0; i < comps.length; i++) { 199 comps[i].removeFocusListener(focusListener); 200 } 201 } 202 focusListener = null; 203 } 204 205 private void initWindowListener() { 206 if (windowListener == null) { 207 if (getWindow() != null) { 208 windowListener = new LocalWindowListener(); 209 getWindow().addWindowListener(windowListener); 210 } 211 } 212 } 213 214 private void clearWindowListener() { 215 Window win = null; 216 217 if (windowListener != null) { 218 if (getWindow() != null) { 219 getWindow().removeWindowListener(windowListener); 220 } 221 } 222 windowListener = null; 223 } 224 225 private class LocalFocusListener extends FocusAdapter { 227 public void focusGained(FocusEvent e) { 228 if (init) { 229 init = false; 230 notifyFirstFocusListeners(); 231 } 232 } 233 234 } 235 private class LocalWindowListener extends WindowAdapter { 236 private boolean invoked = false; 237 238 public void windowClosed(WindowEvent event) { 239 if (!invoked) { 240 saveAndClear(event.getWindow()); 241 } 242 } 243 244 public void windowClosing(WindowEvent event) { 245 invoked = true; 246 saveAndClear(event.getWindow()); 247 } 248 249 private void saveAndClear(Window win) { 250 save(); 251 clearAll(); 252 win.dispose(); 253 } 254 255 } 256 } 257 | Popular Tags |