1 19 20 package org.netbeans.core.spi.multiview; 21 22 import java.awt.BorderLayout ; 23 import java.awt.event.ActionEvent ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.Serializable ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 import javax.swing.AbstractAction ; 31 import javax.swing.Action ; 32 import javax.swing.JButton ; 33 import javax.swing.JComponent ; 34 import javax.swing.JLabel ; 35 import javax.swing.JList ; 36 import javax.swing.JPanel ; 37 import javax.swing.JScrollPane ; 38 import javax.swing.JToolBar ; 39 import javax.swing.text.Document ; 40 import org.netbeans.core.multiview.MultiViewCloneableTopComponent; 41 import org.netbeans.core.multiview.MultiViewTopComponent; 42 import org.openide.DialogDisplayer; 43 import org.openide.NotifyDescriptor; 44 import org.openide.nodes.Node; 45 import org.openide.text.CloneableEditor; 46 import org.openide.text.CloneableEditorSupport; 47 import org.openide.text.NbDocument; 48 import org.openide.util.Lookup; 49 import org.openide.windows.CloneableTopComponent; 50 import org.openide.windows.TopComponent; 51 52 56 public final class MultiViewFactory { 57 58 61 62 public final static MultiViewElement BLANK_ELEMENT = new Blank(); 63 67 public final static Action NOOP_CLOSE_ACTION = new NoopAction(); 68 69 70 71 72 private MultiViewFactory () { 73 } 74 75 82 public static TopComponent createMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) { 83 return createMultiView(descriptions, defaultDesc, createDefaultCloseOpHandler()); 84 } 85 86 94 public static TopComponent createMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc, 95 CloseOperationHandler closeHandler) { 96 if (descriptions == null) return null; 97 if (closeHandler == null) closeHandler = createDefaultCloseOpHandler(); 98 MultiViewTopComponent tc = new MultiViewTopComponent(); 99 tc.setMultiViewDescriptions(descriptions, defaultDesc); 100 tc.setCloseOperationHandler(closeHandler); 101 return tc; 102 } 103 104 109 public static CloneableTopComponent createCloneableMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) { 110 return createCloneableMultiView(descriptions, defaultDesc, createDefaultCloseOpHandler()); 111 } 112 113 119 public static CloneableTopComponent createCloneableMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc, 120 CloseOperationHandler closeHandler) { 121 if (descriptions == null) return null; 122 if (closeHandler == null) closeHandler = createDefaultCloseOpHandler(); 123 MultiViewCloneableTopComponent tc = new MultiViewCloneableTopComponent(); 124 tc.setMultiViewDescriptions(descriptions, defaultDesc); 125 tc.setCloseOperationHandler(closeHandler); 126 return tc; 127 } 128 129 133 134 static CloseOperationState createSafeCloseState() { 135 return new CloseOperationState(true, "ID_CLOSE_OK", NOOP_CLOSE_ACTION, NOOP_CLOSE_ACTION); 136 } 137 138 146 147 public static CloseOperationState createUnsafeCloseState(String warningId, Action proceedAction, Action discardAction) { 148 return new CloseOperationState(false, 149 (warningId == null ? "" : warningId), 150 (proceedAction == null ? NOOP_CLOSE_ACTION : proceedAction), 151 (discardAction == null ? NOOP_CLOSE_ACTION : discardAction)); 152 } 153 154 static CloseOperationHandler createDefaultCloseOpHandler() { 155 return new DefaultCloseHandler(); 156 } 157 158 159 private static final class Blank implements MultiViewElement, Serializable { 160 161 private JPanel panel; 162 private JPanel bar; 163 164 Blank() { 165 panel = new JPanel (); 166 bar = new JPanel (); 167 } 168 169 public void componentActivated() { 170 } 171 172 public void componentClosed() { 173 } 174 175 public void componentDeactivated() { 176 } 177 178 public void componentHidden() { 179 } 180 181 public void componentOpened() { 182 } 183 184 public void componentShowing() { 185 } 186 187 public Action [] getActions() { 188 return new Action [0]; 189 } 190 191 public Lookup getLookup() { 192 return Lookup.EMPTY; 193 } 194 195 public JComponent getToolbarRepresentation() { 196 return bar; 197 } 198 199 public javax.swing.JComponent getVisualRepresentation() { 200 return panel; 201 } 202 203 public void setMultiViewCallback(MultiViewElementCallback callback) { 204 } 205 206 207 public org.openide.awt.UndoRedo getUndoRedo() { 208 return null; 209 } 210 211 public CloseOperationState canCloseElement() { 212 return CloseOperationState.STATE_OK; 213 } 214 215 } 216 217 220 private static final class DefaultCloseHandler implements CloseOperationHandler, Serializable { 221 private static final long serialVersionUID =-3126744916624172427L; 222 223 public boolean resolveCloseOperation(CloseOperationState[] elements) { 224 if (elements != null) { 225 boolean canBeClosed = true; 226 Collection badOnes = new ArrayList (); 227 for (int i = 0; i < elements.length; i++) { 228 if (!elements[i].canClose()) { 229 badOnes.add(elements[i]); 230 canBeClosed = false; 231 } 232 } 233 if (!canBeClosed) { 234 throw new IllegalStateException ("Cannot close component. Some of the elements require close operation handling. See MultiViewFactory.createMultiView()"); 236 } 268 } 269 return true; 270 } 271 272 } 292 293 296 private static final class NoopAction extends AbstractAction { 297 298 public void actionPerformed(java.awt.event.ActionEvent e) { 299 } 301 302 } 303 304 } 305 | Popular Tags |