Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
TabText |
|
| 1.55;1.55 |
1 | /* $Id: TabText.java 17841 2010-01-12 19:17:52Z linus $ | |
2 | ***************************************************************************** | |
3 | * Copyright (c) 2009 Contributors - see below | |
4 | * All rights reserved. This program and the accompanying materials | |
5 | * are made available under the terms of the Eclipse Public License v1.0 | |
6 | * which accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * mvw | |
11 | ***************************************************************************** | |
12 | * | |
13 | * Some portions of this file was previously release using the BSD License: | |
14 | */ | |
15 | ||
16 | // Copyright (c) 1996-2007 The Regents of the University of California. All | |
17 | // Rights Reserved. Permission to use, copy, modify, and distribute this | |
18 | // software and its documentation without fee, and without a written | |
19 | // agreement is hereby granted, provided that the above copyright notice | |
20 | // and this paragraph appear in all copies. This software program and | |
21 | // documentation are copyrighted by The Regents of the University of | |
22 | // California. The software program and documentation are supplied "AS | |
23 | // IS", without any accompanying services from The Regents. The Regents | |
24 | // does not warrant that the operation of the program will be | |
25 | // uninterrupted or error-free. The end-user understands that the program | |
26 | // was developed for research purposes and is advised not to rely | |
27 | // exclusively on the program for any reason. IN NO EVENT SHALL THE | |
28 | // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, | |
29 | // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, | |
30 | // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF | |
31 | // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF | |
32 | // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY | |
33 | // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
34 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE | |
35 | // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF | |
36 | // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, | |
37 | // UPDATES, ENHANCEMENTS, OR MODIFICATIONS. | |
38 | ||
39 | package org.argouml.ui; | |
40 | ||
41 | import java.awt.BorderLayout; | |
42 | import java.awt.Font; | |
43 | import java.util.Collections; | |
44 | ||
45 | import javax.swing.JScrollPane; | |
46 | import javax.swing.JTextArea; | |
47 | import javax.swing.JToolBar; | |
48 | import javax.swing.SwingConstants; | |
49 | import javax.swing.event.DocumentEvent; | |
50 | import javax.swing.event.DocumentListener; | |
51 | ||
52 | import org.apache.log4j.Logger; | |
53 | import org.argouml.application.api.AbstractArgoJPanel; | |
54 | import org.argouml.swingext.UpArrowIcon; | |
55 | import org.argouml.ui.targetmanager.TargetEvent; | |
56 | import org.argouml.ui.targetmanager.TargetManager; | |
57 | import org.tigris.toolbar.ToolBarFactory; | |
58 | ||
59 | /** | |
60 | * A tab that contains textual information. | |
61 | */ | |
62 | public class TabText | |
63 | extends AbstractArgoJPanel | |
64 | implements TabModelTarget, DocumentListener { | |
65 | ||
66 | private Object target; | |
67 | 900 | private JTextArea textArea = new JTextArea(); |
68 | 900 | private boolean parseChanges = true; |
69 | private boolean enabled; | |
70 | ||
71 | /** | |
72 | * The optional toolbar. Contains <code>null</code> if no toolbar | |
73 | * was requested. | |
74 | */ | |
75 | private JToolBar toolbar; | |
76 | ||
77 | /** | |
78 | * Logger. | |
79 | */ | |
80 | 900 | private static final Logger LOG = Logger.getLogger(TabText.class); |
81 | ||
82 | /** | |
83 | * Create a text tab without a toolbar. | |
84 | * | |
85 | * @param title the title of the tab | |
86 | */ | |
87 | public TabText(String title) { | |
88 | 0 | this(title, false); |
89 | 0 | } |
90 | ||
91 | /** | |
92 | * Create a text tab and optionally request a toolbar. | |
93 | * @since ARGO0.9.4 | |
94 | * | |
95 | * @param title the title | |
96 | * @param withToolbar true if a toolbar is needed | |
97 | */ | |
98 | public TabText(String title, boolean withToolbar) { | |
99 | 900 | super(title); |
100 | 900 | setIcon(new UpArrowIcon()); |
101 | 900 | setLayout(new BorderLayout()); |
102 | 900 | textArea.setFont(new Font("Monospaced", Font.PLAIN, 12)); |
103 | 900 | textArea.setTabSize(4); |
104 | 900 | add(new JScrollPane(textArea), BorderLayout.CENTER); |
105 | 900 | textArea.getDocument().addDocumentListener(this); |
106 | ||
107 | // If a toolbar was requested, create an empty one. | |
108 | 900 | if (withToolbar) { |
109 | 900 | toolbar = (new ToolBarFactory(Collections.EMPTY_LIST)) |
110 | .createToolBar(); | |
111 | 900 | toolbar.setOrientation(SwingConstants.HORIZONTAL); |
112 | 900 | toolbar.setFloatable(false); |
113 | 900 | toolbar.setName(getTitle()); |
114 | 900 | add(toolbar, BorderLayout.NORTH); |
115 | } | |
116 | 900 | } |
117 | ||
118 | ||
119 | private void doGenerateText() { | |
120 | 0 | parseChanges = false; |
121 | 0 | if (getTarget() == null) { |
122 | 0 | textArea.setEnabled(false); |
123 | // TODO: Localize | |
124 | 0 | textArea.setText("Nothing selected"); |
125 | 0 | enabled = false; |
126 | } else { | |
127 | 0 | textArea.setEnabled(true); |
128 | 0 | if (isVisible()) { |
129 | 0 | String generatedText = genText(getTarget()); |
130 | 0 | if (generatedText != null) { |
131 | 0 | textArea.setText(generatedText); |
132 | 0 | enabled = true; |
133 | 0 | textArea.setCaretPosition(0); |
134 | } else { | |
135 | 0 | textArea.setEnabled(false); |
136 | // TODO: Localize | |
137 | 0 | textArea.setText("N/A"); |
138 | 0 | enabled = false; |
139 | } | |
140 | } | |
141 | } | |
142 | 0 | parseChanges = true; |
143 | 0 | } |
144 | ||
145 | /* | |
146 | * @see org.argouml.ui.TabTarget#setTarget(java.lang.Object) | |
147 | */ | |
148 | public void setTarget(Object t) { | |
149 | 0 | target = t; |
150 | 0 | if (isVisible()) { |
151 | 0 | doGenerateText(); |
152 | } | |
153 | 0 | } |
154 | ||
155 | /* | |
156 | * Returns the target of this tab. | |
157 | * | |
158 | * @see org.argouml.ui.TabTarget#getTarget() | |
159 | */ | |
160 | public Object getTarget() { | |
161 | 0 | return target; |
162 | } | |
163 | ||
164 | /* | |
165 | * Refresh the text of the tab. | |
166 | * | |
167 | * @see org.argouml.ui.TabTarget#refresh() | |
168 | */ | |
169 | public void refresh() { | |
170 | 0 | Object t = TargetManager.getInstance().getTarget(); |
171 | 0 | setTarget(t); |
172 | 0 | } |
173 | ||
174 | /** | |
175 | * This tab pane is enabled if there is a target, | |
176 | * i.e. the target must not be null. | |
177 | * | |
178 | * {@inheritDoc} | |
179 | */ | |
180 | public boolean shouldBeEnabled(Object t) { | |
181 | 0 | return (t != null); |
182 | } | |
183 | ||
184 | /** | |
185 | * The target has changed, so let's generate some text to be shown. | |
186 | * | |
187 | * @param t the object to be "generated" = make a string of it | |
188 | * @return the generated text | |
189 | */ | |
190 | protected String genText(Object t) { | |
191 | 0 | return t == null ? "Nothing selected" : t.toString(); |
192 | } | |
193 | ||
194 | /** | |
195 | * The user has edited the text in the textfield, so let's parse it now, | |
196 | * and update the model. | |
197 | * | |
198 | * @param s the string to parse | |
199 | */ | |
200 | protected void parseText(String s) { | |
201 | 0 | if (s == null) { |
202 | 0 | s = "(null)"; |
203 | } | |
204 | 0 | LOG.debug("parsing text:" + s); |
205 | 0 | } |
206 | ||
207 | /* | |
208 | * @see javax.swing.event.DocumentListener#insertUpdate(javax.swing.event.DocumentEvent) | |
209 | */ | |
210 | public void insertUpdate(DocumentEvent e) { | |
211 | 0 | if (parseChanges) { |
212 | 0 | parseText(textArea.getText()); |
213 | } | |
214 | 0 | } |
215 | ||
216 | /* | |
217 | * @see javax.swing.event.DocumentListener#removeUpdate(javax.swing.event.DocumentEvent) | |
218 | */ | |
219 | public void removeUpdate(DocumentEvent e) { | |
220 | 0 | if (parseChanges) { |
221 | 0 | parseText(textArea.getText()); |
222 | } | |
223 | 0 | } |
224 | ||
225 | /* | |
226 | * @see javax.swing.event.DocumentListener#changedUpdate(javax.swing.event.DocumentEvent) | |
227 | */ | |
228 | public void changedUpdate(DocumentEvent e) { | |
229 | 0 | if (parseChanges) { |
230 | 0 | parseText(textArea.getText()); |
231 | } | |
232 | 0 | } |
233 | ||
234 | /* | |
235 | * @see org.argouml.ui.targetmanager.TargetListener#targetAdded(org.argouml.ui.targetmanager.TargetEvent) | |
236 | */ | |
237 | public void targetAdded(TargetEvent e) { | |
238 | 0 | setTarget(e.getNewTarget()); |
239 | ||
240 | 0 | } |
241 | ||
242 | /* | |
243 | * @see org.argouml.ui.targetmanager.TargetListener#targetRemoved(org.argouml.ui.targetmanager.TargetEvent) | |
244 | */ | |
245 | public void targetRemoved(TargetEvent e) { | |
246 | // how to handle empty target lists? | |
247 | // probably the TabText should only show an empty pane in that case | |
248 | 0 | setTarget(e.getNewTarget()); |
249 | ||
250 | 0 | } |
251 | ||
252 | /* | |
253 | * @see org.argouml.ui.targetmanager.TargetListener#targetSet(org.argouml.ui.targetmanager.TargetEvent) | |
254 | */ | |
255 | public void targetSet(TargetEvent e) { | |
256 | 0 | setTarget(e.getNewTarget()); |
257 | ||
258 | 0 | } |
259 | ||
260 | /** | |
261 | * @return Returns the toolbar. | |
262 | */ | |
263 | protected JToolBar getToolbar() { | |
264 | 3600 | return toolbar; |
265 | } | |
266 | ||
267 | /** | |
268 | * @param s true if we are enabled | |
269 | */ | |
270 | protected void setShouldBeEnabled(boolean s) { | |
271 | 1327 | this.enabled = s; |
272 | 1327 | } |
273 | ||
274 | /** | |
275 | * @return returns true if enabled | |
276 | */ | |
277 | protected boolean shouldBeEnabled() { | |
278 | 1327 | return enabled; |
279 | } | |
280 | ||
281 | /** | |
282 | * Sets if text area can be edited. | |
283 | * @param editable if true, text area is editable (default), else not | |
284 | */ | |
285 | public void setEditable(boolean editable) { | |
286 | 900 | textArea.setEditable(editable); |
287 | 900 | } |
288 | ||
289 | /** | |
290 | * Generates the text whenever this panel becomes visible. | |
291 | * {@inheritDoc} | |
292 | */ | |
293 | @Override | |
294 | public void setVisible(boolean visible) { | |
295 | 900 | super.setVisible(visible); |
296 | 900 | if (visible) { |
297 | 0 | doGenerateText(); |
298 | } | |
299 | 900 | } |
300 | ||
301 | /** | |
302 | * The UID. | |
303 | */ | |
304 | private static final long serialVersionUID = -1484647093166393888L; | |
305 | } /* end class TabText */ |