KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > ui > AbstractOutputTab


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.ui;
21
22 import javax.accessibility.Accessible JavaDoc;
23 import javax.accessibility.AccessibleContext JavaDoc;
24 import javax.accessibility.AccessibleRole JavaDoc;
25 import org.netbeans.core.output2.Controller;
26
27 import javax.swing.*;
28 import javax.swing.text.Document JavaDoc;
29 import java.awt.*;
30 import java.awt.event.ActionEvent JavaDoc;
31 import java.awt.event.ActionListener JavaDoc;
32
33 /**
34  * A basic output pane. This class implements the non-output window specific
35  * gui management for the output window - creating the text component,
36  * locking the caret and scrollbar to the bottom of the document to the
37  * bottom, etc. Could be merged with OutputView, but it's more readable
38  * and maintainable to keep the pure gui code separate. Mainly contains
39  * logic for layout and showing and hiding a toolbar and input area.
40  *
41  * @author Tim Boudreau
42  */

43 public abstract class AbstractOutputTab extends JComponent implements ActionListener JavaDoc, Accessible JavaDoc {
44     private InputPanel input = null;
45     private AbstractOutputPane outputPane;
46     private Action[] actions = new Action[0];
47     private JButton[] buttons = new JButton[0];
48     
49     private Component toFocus;
50     
51     public AbstractOutputTab() {
52         outputPane = createOutputPane();
53         add (outputPane);
54         setFocusable(false);
55     }
56     
57     public void setDocument (Document JavaDoc doc) {
58         outputPane.setDocument(doc);
59     }
60     
61     /* Read accessible context
62      * @return - accessible context
63      */

64     public AccessibleContext JavaDoc getAccessibleContext() {
65         if (accessibleContext == null) {
66             accessibleContext = new AccessibleJComponent() {
67                         public AccessibleRole JavaDoc getAccessibleRole() {
68                             // is it really a panel?
69
return AccessibleRole.PANEL;
70                         }
71
72                         public String JavaDoc getAccessibleName() {
73                             if (accessibleName != null) {
74                                 return accessibleName;
75                             }
76                             return getName();
77                         }
78                     };
79         }
80
81         return accessibleContext;
82     }
83     
84
85     /**
86      * on mouse click the specialized component is marked, and activation is requested.
87      * activation results in request focus on the tab -> the marked component gets focus.
88      */

89     public void setToFocus(Component foc) {
90         toFocus = foc;
91     }
92     
93     public void requestFocus() {
94     // on mouse click the specialized component is marked, and activation is requested.
95
// activation results in request focus on the tab -> the marked component gets focus.
96
if (toFocus != null) {
97             toFocus.requestFocus();
98             toFocus = null;
99             return;
100         }
101         if (isInputVisible()) {
102             input.requestFocus();
103         } else {
104             outputPane.requestFocus();
105         }
106     }
107     
108     public boolean requestFocusInWindow() {
109         if (isInputVisible()) {
110             return input.requestFocusInWindow();
111         } else {
112             return getOutputPane().requestFocusInWindow();
113         }
114     }
115
116     protected abstract AbstractOutputPane createOutputPane();
117     
118     protected abstract void inputSent (String JavaDoc txt);
119     
120     public final AbstractOutputPane getOutputPane() {
121         return outputPane;
122     }
123
124     public final void setToolbarActions (Action[] a) {
125         if (a == null || a.length == 0) {
126             actions = new Action[0];
127             buttons = new JButton[0];
128             return;
129         }
130         if (a.length > 5) {
131             throw new IllegalArgumentException JavaDoc ("No more than 5 actions allowed" //NOI18N
132
+ "in the output window toolbar"); //NOI18N
133
}
134         actions = new Action[a.length];
135         buttons = new JButton[a.length];
136         for (int i=0; i < buttons.length; i++) {
137             actions[i] = a[i];
138             // mkleint - ignore the WeakAction referencing as it introduces
139
// additional non obvious contract to using the the toolbar actions.
140
// actions[i] = new WeakAction(a[i]);
141
installKeyboardAction (actions[i]);
142             buttons[i] = new JButton(actions[i]);
143             buttons[i].setBorderPainted(false);
144             buttons[i].setOpaque(false);
145             buttons[i].setText(null);
146             buttons[i].putClientProperty("hideActionText", Boolean.TRUE); //NOI18N
147
if (a[i].getValue (Action.SMALL_ICON) == null) {
148                 throw new IllegalStateException JavaDoc ("No icon provided for " + a[i]); //NOI18N
149
}
150         }
151     }
152
153
154     /**
155      * Get the toolbar actions, if any, which have been supplied by the client.
156      * Used to add them to the popup menu if they return a non-null name.
157      *
158      * @return An array of actions
159      */

160     public Action[] getToolbarActions() {
161         return actions;
162     }
163
164     /**
165      * Install a keyboard action. This is used in two places - all toolbar actions with
166      * accelerator keys and names will also be installed as keyboard actions. Also, the
167      * master controller installs its actions which should be accessible via the keyboard.
168      * The actions are actually installed into the text control.
169      *
170      * @param a An action to install, if its name and accelerator are non-null
171      */

172     public void installKeyboardAction (Action a) {
173         if (!(a instanceof WeakAction)) {
174             //It is a Controller.ControllerAction - don't create a memory leak by listening to it
175
a = new WeakAction(a);
176         }
177         KeyStroke accel = null;
178         String JavaDoc name;
179         Object JavaDoc o = a.getValue (Action.ACCELERATOR_KEY);
180         if (o instanceof KeyStroke) {
181             accel = (KeyStroke) o;
182         }
183         name = (String JavaDoc) a.getValue(Action.NAME);
184         if (accel != null) {
185             if (Controller.LOG) Controller.log ("Installed action " + name + " on " + accel);
186             // if the logic here changes, check the popup escaping hack in Controller
187
// it temporarily removes the VK_ESCAPE from input maps..
188
JComponent c = getOutputPane().textView;
189             c.getInputMap().put(accel, name);
190             c.getActionMap().put(name, a);
191             getInputMap (WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put (accel, name);
192             getActionMap().put(name, a);
193         }
194     }
195
196     public final boolean isInputVisible() {
197         return input != null && input.getParent() == this && input.isVisible();
198     }
199     
200     public final void setInputVisible (boolean val) {
201         if (val == isInputVisible()) {
202             return;
203         }
204         if (val) {
205             if (input == null) {
206                 input = new InputPanel();
207                 input.addActionListener(this);
208             }
209             if (input.getParent() != this) {
210                 add (input);
211                 validate();
212             }
213         }
214         input.setVisible (val);
215         validate();
216         getOutputPane().ensureCaretPosition();
217     }
218     
219     public void actionPerformed(ActionEvent JavaDoc ae) {
220         InputPanel ip = (InputPanel) ae.getSource();
221         if (InputPanel.ACTION_EOF.equals(ae.getActionCommand())) {
222             inputEof();
223         } else {
224             inputSent (ip.getText());
225         }
226     }
227
228     protected abstract void inputEof();
229
230     public void doLayout() {
231         boolean hasInput = isInputVisible();
232         Insets ins = getInsets();
233         int left = ins.left;
234         int bottom = hasInput ? (getHeight() - ins.bottom -
235             (input.getPreferredSize().height)) - 3 : getHeight() - ins.bottom;
236         
237         Component main = outputPane;
238         
239         if (main != null) {
240             main.setBounds (left, ins.top, getWidth() - (left + ins.right),
241                 bottom - ins.top);
242         }
243         if (hasInput) {
244             input.setBounds (left, bottom, getWidth() - (left + ins.right),
245                 getHeight() - bottom);
246         }
247     }
248
249     public abstract void hasSelectionChanged(boolean val);
250     
251     void notifyInputFocusGained(){
252         getOutputPane().lockScroll();
253         getOutputPane().ensureCaretPosition();
254     }
255
256     JButton[] getToolbarButtons() {
257         return buttons;
258     }
259
260 }
261
Popular Tags