KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > OutputWindow


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.output2;
21
22 import java.awt.AWTEvent JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Graphics JavaDoc;
26 import java.awt.Insets JavaDoc;
27 import java.awt.Point JavaDoc;
28 import java.awt.event.FocusEvent JavaDoc;
29 import java.awt.event.MouseAdapter JavaDoc;
30 import java.awt.event.MouseEvent JavaDoc;
31 import java.awt.event.MouseListener JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import javax.swing.UIManager JavaDoc;
36 import org.netbeans.core.output2.ui.AbstractOutputTab;
37 import org.netbeans.core.output2.ui.AbstractOutputWindow;
38 import org.openide.util.NbBundle;
39 import org.openide.util.Utilities;
40 import org.openide.windows.TopComponent;
41 import org.openide.windows.WindowManager;
42
43 /**
44  * An output window. Note this class contains no logic of interest - all
45  * events of interest are passed to the <code>Controller</code> which
46  * manages this instance (and possibly others).
47  * <p>
48  * The mechanism for displaying/not displaying the tabbed pane is handled in
49  * the superclass, which overrides addImpl() and remove() to automatically install
50  * the tabbed pane if more than one view is added, and remove it if only one
51  * is present - so it is enough to simply call add() and remove() with instances
52  * of OutputTab and the management of tabs will be taken care of automatically.
53  */

54 public class OutputWindow extends AbstractOutputWindow {
55     private Controller controller;
56     static OutputWindow DEFAULT = null;
57     public static final String JavaDoc ICON_RESOURCE =
58         "org/netbeans/core/resources/frames/output.gif"; // NOI18N
59

60     private MouseListener JavaDoc activateListener = new MouseAdapter JavaDoc() {
61         public void mouseClicked(MouseEvent JavaDoc e) {
62             //#83829
63
requestActive();
64         }
65     };
66
67     public OutputWindow() {
68         this (new Controller());
69         enableEvents(AWTEvent.FOCUS_EVENT_MASK);
70         putClientProperty ("dontActivate", Boolean.TRUE);
71         getActionMap().put("PreviousViewAction", controller.prevTabAction);
72         getActionMap().put("NextViewAction", controller.nextTabAction);
73     }
74     
75     public void addNotify() {
76         super.addNotify();
77         pane.addMouseListener(activateListener);
78     }
79
80     public void removeNotify() {
81         super.removeNotify();
82         pane.removeMouseListener(activateListener);
83     }
84     
85
86     protected void closeRequest(AbstractOutputTab tab) {
87         controller.close (this, (OutputTab) tab, false);
88     }
89
90     OutputWindow (Controller controller) {
91         if (Controller.LOG) Controller.log("Created an output window");
92         this.controller = controller;
93         setDisplayName (NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT")); //NOI18N
94
// setting name to satisfy the accesible name requirement for window.
95
setName (NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT")); //NOI18N
96

97         setIcon(Utilities.loadImage(ICON_RESOURCE)); // NOI18N
98
// special title for sliding mode
99
// XXX - please rewrite to regular API when available - see issue #55955
100
putClientProperty("SlidingName", getDisplayName()); //NOI18N
101
}
102     
103     public static synchronized OutputWindow findDefault() {
104         if (DEFAULT == null) {
105             //If settings file is correctly defined call of WindowManager.findTopComponent() will
106
//call TestComponent00.getDefault() and it will set static field component.
107
TopComponent tc = WindowManager.getDefault().findTopComponent("output"); // NOI18N
108
if (tc != null) {
109                 if (!(tc instanceof OutputWindow)) {
110                     //This should not happen. Possible only if some other module
111
//defines different settings file with the same name but different class.
112
//Incorrect settings file?
113
IllegalStateException JavaDoc exc = new IllegalStateException JavaDoc
114                     ("Incorrect settings file. Unexpected class returned." // NOI18N
115
+ " Expected:" + OutputWindow.class.getName() // NOI18N
116
+ " Returned:" + tc.getClass().getName()); // NOI18N
117
Logger.getLogger(OutputWindow.class.getName()).log(Level.WARNING, null, exc);
118                     //Fallback to accessor reserved for window system.
119
OutputWindow.getDefault();
120                 }
121             } else {
122                 OutputWindow.getDefault();
123             }
124         }
125         return DEFAULT;
126     }
127     /* Singleton accessor reserved for window system ONLY. Used by window system to create
128      * OutputWindow instance from settings file when method is given. Use <code>findDefault</code>
129      * to get correctly deserialized instance of OutputWindow. */

130     public static synchronized OutputWindow getDefault() {
131         if (DEFAULT == null) {
132             DEFAULT = new OutputWindow();
133         }
134         return DEFAULT;
135     }
136
137     public int getPersistenceType() {
138         return PERSISTENCE_ALWAYS;
139     }
140
141     public String JavaDoc preferredID() {
142         return "output"; //NOI18N
143
}
144
145     public Object JavaDoc readResolve() throws java.io.ObjectStreamException JavaDoc {
146         return getDefault();
147     }
148     
149     public String JavaDoc getToolTipText() {
150         return getDisplayName();
151     }
152
153     Controller getController() {
154         return controller;
155     }
156
157     public void requestVisible () {
158         if (Controller.LOG) {
159             Controller.log("Request visible");
160             Controller.logStack();
161         }
162         super.requestVisible();
163     }
164     
165     void requestVisibleForNewTab() {
166         if (Controller.LOG) Controller.log("Request visible for new tab");
167         if (isOpened() && isShowing()) {
168             if (!isActivated()) {
169                 super.requestVisible();
170             }
171         } else {
172             if (Controller.LOG) Controller.log ("CALLING OPEN() ON OUTPUT WINDOW!");
173             open();
174             super.requestVisible();
175             if (Boolean.TRUE.equals(getClientProperty("isSliding"))) { //NOI18N
176
requestActiveForNewTab();
177             }
178         }
179     }
180     
181     public void processFocusEvent (FocusEvent JavaDoc fe) {
182         super.processFocusEvent (fe);
183         if (Boolean.TRUE.equals(getClientProperty("isSliding"))) { //NOI18N
184
repaint(200);
185         }
186     }
187     
188     public void paintComponent (Graphics JavaDoc g) {
189         super.paintComponent (g);
190         if (hasFocus()) {
191             Insets JavaDoc ins = getInsets();
192             Color JavaDoc col = UIManager.getColor ("controlShadow"); //NOI18N
193
//Draw *some* focus indication
194
if (col == null) col = Color.GRAY;
195             g.setColor(col);
196             g.drawRect (
197                 ins.left + 2,
198                 ins.top + 2,
199                 getWidth() - (ins.left + ins.right + 4),
200                 getHeight() - (ins.top + ins.bottom + 4));
201         }
202     }
203     
204     void requestActiveForNewTab() {
205         requestActive();
206     }
207     
208     public void requestActive() {
209         boolean activated = isActivated();
210         if (Controller.LOG) Controller.log("Request active");
211         super.requestActive();
212         if (!activated) {
213             requestFocus();
214         }
215     }
216     
217     private boolean activated = false;
218     protected void componentActivated () {
219         if (Controller.LOG) Controller.log("ComponentActivated");
220         super.componentActivated();
221         activated = true;
222         controller.notifyActivated (this);
223         requestFocus();
224     }
225     
226     protected void componentDeactivated() {
227         if (Controller.LOG) Controller.log("ComponentDeactivated");
228         super.componentDeactivated();
229         activated = false;
230     }
231     
232     protected void removed(AbstractOutputTab view) {
233         if (Controller.LOG) Controller.log("Removed tab " + view);
234         if (Controller.LOG) Controller.log ("Tab has been removed. Notifying controller.");
235         controller.notifyRemoved((OutputTab) view);
236     }
237
238     protected void selectionChanged(AbstractOutputTab former, AbstractOutputTab current) {
239         controller.selectionChanged (this, (OutputTab) former, (OutputTab) current);
240     }
241
242     public void lineClicked(OutputTab outputComponent, int line) {
243         controller.lineClicked (this, outputComponent, line);
244     }
245
246     public void postPopupMenu(OutputTab outputComponent, Point JavaDoc p, Component JavaDoc src) {
247         controller.postPopupMenu (this, outputComponent, p, src);
248     }
249
250     public void caretEnteredLine(OutputTab outputComponent, int line) {
251         controller.caretEnteredLine(outputComponent, line);
252     }
253
254     public void documentChanged(OutputTab comp) {
255         controller.documentChanged (this, comp);
256     }
257
258     private HashSet JavaDoc hiddenTabs = null;
259     public void putHiddenView (OutputTab comp) {
260         if (hiddenTabs == null) {
261             hiddenTabs = new HashSet JavaDoc();
262         }
263         comp.putClientProperty("outputWindow", this); //NOI18N
264
hiddenTabs.add(comp);
265         if (comp.getParent() != null) {
266             comp.getParent().remove(comp);
267         }
268     }
269
270     public void removeHiddenView (OutputTab comp) {
271         hiddenTabs.remove(comp);
272         comp.putClientProperty("outputWindow", null); //NOI18N
273
}
274
275     public void setSelectedTab (AbstractOutputTab op) {
276         if (op.getParent() == null && hiddenTabs.contains(op)) {
277             removeHiddenView ((OutputTab) op);
278             add(op);
279         }
280         super.setSelectedTab (op);
281     }
282
283     protected void updateSingletonName(String JavaDoc name) {
284         String JavaDoc winName = NbBundle.getMessage(OutputWindow.class, "LBL_OUTPUT"); //NOI18N
285
if (name != null) {
286             String JavaDoc newName = NbBundle.getMessage(OutputWindow.class,
287                 "FMT_OUTPUT", new Object JavaDoc[] {winName, name}); //NOI18N
288
if (newName.indexOf ("<html>") != -1) {
289                 newName = Utilities.replaceString(newName, "<html>", ""); //NOI18N
290
setHtmlDisplayName("<html>" + newName); //NOI18N
291
} else {
292                 setDisplayName(newName);
293                 setHtmlDisplayName(null);
294             }
295         } else {
296             setDisplayName(winName);
297             setHtmlDisplayName(null);
298         }
299     }
300
301
302     public OutputTab[] getHiddenTabs() {
303         if (hiddenTabs != null && !hiddenTabs.isEmpty()) {
304             OutputTab[] result = new OutputTab[hiddenTabs.size()];
305             return (OutputTab[]) hiddenTabs.toArray(result);
306         }
307         return new OutputTab[0];
308     }
309
310     public OutputTab getTabForIO (NbIO io) {
311         AbstractOutputTab[] views = getTabs();
312         for (int i=0; i < views.length; i++) {
313             if (((OutputTab) views[i]).getIO() == io) {
314                 return ((OutputTab) views[i]);
315             }
316         }
317         OutputTab[] hidden = getHiddenTabs();
318         for (int i=0; i < hidden.length; i++) {
319             if (hidden[i].getIO() == io) {
320                 return hidden[i];
321             }
322         }
323         return null;
324     }
325
326     public void eventDispatched(IOEvent ioe) {
327             if (Controller.LOG) Controller.log ("Event received: " + ioe);
328             NbIO io = ioe.getIO();
329             int command = ioe.getCommand();
330             boolean value = ioe.getValue();
331             Object JavaDoc data = ioe.getData();
332             OutputTab comp = getTabForIO (io);
333             if (command == IOEvent.CMD_DETACH) {
334                 if (!ioe.isConsumed()) {
335                     //Can be used by ModuleInstall to dispose of the current output window if desired
336
ioe.consume();
337                     DEFAULT = null;
338                     return;
339                 }
340             }
341             if (Controller.LOG) Controller.log ("Passing command to controller " + ioe);
342             controller.performCommand (this, comp, io, command, value, data);
343             ioe.consume();
344     }
345
346     public void hasSelectionChanged(OutputTab tab, boolean val) {
347         controller.hasSelectionChanged(this, tab, val);
348     }
349
350     public boolean isActivated() {
351         return activated;
352     }
353
354     public void hasOutputListenersChanged(OutputTab tab, boolean hasOutputListeners) {
355         controller.hasOutputListenersChanged(this, tab, hasOutputListeners);
356     }
357
358     public void inputEof(OutputTab tab) {
359         if (Controller.LOG) Controller.log ("Input EOF on " + this);
360         controller.inputEof(tab);
361     }
362
363     public void inputSent(OutputTab c, String JavaDoc txt) {
364         if (Controller.LOG) Controller.log ("Notifying controller input sent " + txt);
365         controller.notifyInput(this, c, txt);
366     }
367 }
368
Popular Tags