KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > spi > multiview > MultiViewFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.spi.multiview;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import javax.swing.AbstractAction JavaDoc;
31 import javax.swing.Action JavaDoc;
32 import javax.swing.JButton JavaDoc;
33 import javax.swing.JComponent JavaDoc;
34 import javax.swing.JLabel JavaDoc;
35 import javax.swing.JList JavaDoc;
36 import javax.swing.JPanel JavaDoc;
37 import javax.swing.JScrollPane JavaDoc;
38 import javax.swing.JToolBar JavaDoc;
39 import javax.swing.text.Document JavaDoc;
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 /** Factory class for creating top components handling multi views.
53  *
54  * @author Dafe Simonek, Milos Kleint
55  */

56 public final class MultiViewFactory {
57     
58     /**
59      * A utility singleton instance of MultiViewElement that does nothing.
60      */

61     
62     public final static MultiViewElement BLANK_ELEMENT = new Blank();
63     /**
64      * a utility noop action instance to be used when no special handling is
65      * required in createUnsafeCloseState() method.
66      */

67     public final static Action JavaDoc NOOP_CLOSE_ACTION = new NoopAction();
68     
69     
70
71     /** Factory class, no instances. */
72     private MultiViewFactory () {
73     }
74
75     /** Creates and returns new instance of top component with
76      * multi views.
77      * PLEASE NOTE: a non-cloneable TopComponent is not able to embed editors aka subclasses of CloneableEditor correctly.
78      * Use createCloneableMultiView() method in such a case.
79      * @param descriptions array of descriptions of tabs in the multiview.
80      * @param defaultDesc the initial selection, one of the descriptions array values.
81      */

82     public static TopComponent createMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) {
83         return createMultiView(descriptions, defaultDesc, createDefaultCloseOpHandler());
84     }
85
86     /** Creates and returns new instance of top component with
87      * multi views.
88      * PLEASE NOTE: a non-cloneable TopComponent is not able to embed editors aka subclasses of CloneableEditor correctly.
89      * Use createCloneableMultiView() method in such a case.
90      * @param descriptions array of descriptions of tabs in the multiview.
91      * @param defaultDesc the initial selection, one of the descriptions array values.
92      * @param closeHandler handles closing of the multiview component, useful when any of the embedded elements can be in modified state and closing would cause a dataloss..
93      */

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    /** Creates and returns new instance of cloneable top component with
105      * multi views
106      * @param descriptions array of descriptions of tabs in the multiview.
107      * @param defaultDesc the initial selection, one of the descriptions array values.
108     */

109     public static CloneableTopComponent createCloneableMultiView (MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) {
110         return createCloneableMultiView(descriptions, defaultDesc, createDefaultCloseOpHandler());
111     }
112
113     /** Creates and returns new instance of cloneable top component with
114      * multi views.
115      * @param descriptions array of descriptions of tabs in the multiview.
116      * @param defaultDesc the initial selection, one of the descriptions array values.
117      * @param closeHandler handles closing of the multiview component, useful when any of the embedded elements can be in modified state and closing would cause a dataloss..
118      */

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     /**
130      * Utility method for MultiViewElements to create a CloseOperationState instance that
131      * informs the environment that the MVElement is ok to be closed.
132      */

133     
134     static CloseOperationState createSafeCloseState() {
135         return new CloseOperationState(true, "ID_CLOSE_OK", NOOP_CLOSE_ACTION, NOOP_CLOSE_ACTION);
136     }
137
138     /**
139      * Utility method for MultiViewElements to create a CloseOperationState instance
140      * that warns about possible data loss. Corrective actions can be defined.
141      * @param warningId an id that idenfifies the problem,
142      * the CloseOperationHandler used in the component should know about the warning's meaning and handle appropriately
143      * @param proceedAction will be performed when the CloseOperationHandler decides that closing the component is ok and changes are to be saved.
144      * @param discardAction will be performed when the CloseOperationHandler decides that the nonsaved data shall be discarded
145      */

146     
147     public static CloseOperationState createUnsafeCloseState(String JavaDoc warningId, Action JavaDoc proceedAction, Action JavaDoc 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 JavaDoc {
160         
161         private JPanel JavaDoc panel;
162         private JPanel JavaDoc bar;
163         
164         Blank() {
165             panel = new JPanel JavaDoc();
166             bar = new JPanel JavaDoc();
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 JavaDoc[] getActions() {
188             return new Action JavaDoc[0];
189         }
190         
191         public Lookup getLookup() {
192             return Lookup.EMPTY;
193         }
194         
195         public JComponent JavaDoc getToolbarRepresentation() {
196             return bar;
197         }
198         
199         public javax.swing.JComponent JavaDoc 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 /**
218  * default simple implementation of the close handler.
219  */

220     private static final class DefaultCloseHandler implements CloseOperationHandler, Serializable JavaDoc {
221          private static final long serialVersionUID =-3126744916624172427L;
222        
223         public boolean resolveCloseOperation(CloseOperationState[] elements) {
224             if (elements != null) {
225                 boolean canBeClosed = true;
226                 Collection JavaDoc badOnes = new ArrayList JavaDoc();
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                     //TODO SHOW dialog here.
235
throw new IllegalStateException JavaDoc("Cannot close component. Some of the elements require close operation handling. See MultiViewFactory.createMultiView()");
236 // Object[] options = new Object[] {
237
// new JButton("Proceed"),
238
// new JButton("Discard"),
239
// new JButton("Cancel")
240
// };
241
// NotifyDescriptor desc = new NotifyDescriptor(createPanel(badOnes), "Cannot close component.",
242
// NotifyDescriptor.DEFAULT_OPTION, NotifyDescriptor.WARNING_MESSAGE,
243
// options, options[0]);
244
// Object retVal = DialogDisplayer.getDefault().notify(desc);
245
// if (retVal == options[0]) {
246
// // do proceed.
247
// Iterator it = badOnes.iterator();
248
// while (it.hasNext()) {
249
// Action act = ((CloseOperationState)it.next()).getProceedAction();
250
// if (act != null) {
251
// act.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "proceed"));
252
// }
253
// }
254
// } else if (retVal == options[1]) {
255
// // do discard
256
// Iterator it = badOnes.iterator();
257
// while (it.hasNext()) {
258
// Action act = ((CloseOperationState)it.next()).getDiscardAction();
259
// if (act != null) {
260
// act.actionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED, "discard"));
261
// }
262
// }
263
// } else {
264
// // was cancel..
265
// return false;
266
// }
267
}
268             }
269             return true;
270         }
271         
272 // private JPanel createPanel(Collection elems) {
273
// JPanel panel = new JPanel();
274
// panel.setLayout(new BorderLayout());
275
// JLabel lbl = new JLabel("Cannot safely close component for following reasons:");
276
// panel.add(lbl, BorderLayout.NORTH);
277
// JScrollPane pane = new JScrollPane();
278
// String[] warnings = new String[elems.size()];
279
// int index = 0;
280
// Iterator it = elems.iterator();
281
// while (it.hasNext()) {
282
// CloseOperationState state = (CloseOperationState)it.next();
283
// warnings[index] = state.getCloseWarningMessage();
284
// index = index + 1;
285
// }
286
// JList list = new JList(warnings);
287
// pane.setViewportView(list);
288
// panel.add(pane);
289
// return panel;
290
// }
291
}
292     
293     /**
294      * just a default noon action to put into the closeoperation state.
295      */

296     private static final class NoopAction extends AbstractAction JavaDoc {
297         
298         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
299             // do nothing
300
}
301         
302     }
303     
304 }
305
Popular Tags