001: /*
002: * The Unified Mapping Platform (JUMP) is an extensible, interactive GUI
003: * for visualizing and manipulating spatial features with geometry and attributes.
004: *
005: * Copyright (C) 2003 Vivid Solutions
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: *
021: * For more information, contact:
022: *
023: * Vivid Solutions
024: * Suite #1A
025: * 2328 Government Street
026: * Victoria BC V8T 5G5
027: * Canada
028: *
029: * (250)385-6040
030: * www.vividsolutions.com
031: */
032:
033: package com.vividsolutions.jump.workbench.plugin;
034:
035: import java.util.Arrays;
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: import javax.swing.JComponent;
040: import javax.swing.JInternalFrame;
041:
042: import com.vividsolutions.jts.util.Assert;
043: import com.vividsolutions.jump.util.StringUtil;
044: import com.vividsolutions.jump.workbench.WorkbenchContext;
045: import com.vividsolutions.jump.workbench.model.Layer;
046: import com.vividsolutions.jump.workbench.model.LayerManager;
047: import com.vividsolutions.jump.workbench.model.LayerManagerProxy;
048: import com.vividsolutions.jump.workbench.ui.LayerNamePanel;
049: import com.vividsolutions.jump.workbench.ui.LayerNamePanelProxy;
050: import com.vividsolutions.jump.workbench.ui.LayerViewFrame;
051: import com.vividsolutions.jump.workbench.ui.LayerViewPanel;
052: import com.vividsolutions.jump.workbench.ui.LayerViewPanelProxy;
053: import com.vividsolutions.jump.workbench.ui.SelectionManager;
054: import com.vividsolutions.jump.workbench.ui.SelectionManagerProxy;
055: import com.vividsolutions.jump.workbench.ui.TaskFrame;
056: import com.vividsolutions.jump.workbench.ui.TaskFrameProxy;
057: import com.vividsolutions.jump.workbench.ui.warp.WarpingVectorLayerFinder;
058:
059: /**
060: * Creates basic EnableChecks.
061: * @see EnableCheck
062: */
063: public class EnableCheckFactory {
064: private WorkbenchContext workbenchContext;
065:
066: public EnableCheckFactory(WorkbenchContext workbenchContext) {
067: Assert.isTrue(workbenchContext != null);
068: this .workbenchContext = workbenchContext;
069: }
070:
071: //<<TODO:WORKAROUND>> I came across a bug in the JBuilder 4 compiler (bcj.exe)
072: //that occurs when using the Java ternary operator ( ? : ). For it to
073: //happen, [1] the middle operand must be null and [2] an inner class
074: //must be nearby. For example, try using JBuilder to compile the following code:
075: //
076: // import java.awt.event.WindowAdapter;
077: // public class TestClass {
078: // static public void main(String[] args) {
079: // System.out.println(true ? null : "FALSE");
080: // WindowAdapter w = new WindowAdapter() { };
081: // }
082: // }
083: //
084: //You'd expect it to print out "null", but "FALSE" is printed! And if you comment
085: //out the line with the anonymous inner class (WindowAdapter w), it prints out
086: //"null" as expected! I've submitted a bug report to Borland (case number 488569).
087: //
088: //So, if you're using JBuilder, don't use ?: if the middle operand could be null!
089: //[Jon Aquino]
090: public EnableCheck createTaskWindowMustBeActiveCheck() {
091: return new EnableCheck() {
092: public String check(JComponent component) {
093: return (!(workbenchContext.getWorkbench().getFrame()
094: .getActiveInternalFrame() instanceof TaskFrame)) ? "A Task Window must be active"
095: : null;
096: }
097: };
098: }
099:
100: public EnableCheck createWindowWithSelectionManagerMustBeActiveCheck() {
101: return new EnableCheck() {
102: public String check(JComponent component) {
103: return (!(workbenchContext.getWorkbench().getFrame()
104: .getActiveInternalFrame() instanceof SelectionManagerProxy)) ? "A window with a selection manager must be active"
105: : null;
106: }
107: };
108: }
109:
110: public EnableCheck createWindowWithLayerManagerMustBeActiveCheck() {
111: return new EnableCheck() {
112: public String check(JComponent component) {
113: return (!(workbenchContext.getWorkbench().getFrame()
114: .getActiveInternalFrame() instanceof LayerManagerProxy)) ? "A window with a layer manager must be active"
115: : null;
116: }
117: };
118: }
119:
120: public EnableCheck createWindowWithAssociatedTaskFrameMustBeActiveCheck() {
121: return new EnableCheck() {
122: public String check(JComponent component) {
123: return (!(workbenchContext.getWorkbench().getFrame()
124: .getActiveInternalFrame() instanceof TaskFrameProxy)) ? "A window with an associated task frame must be active"
125: : null;
126: }
127: };
128: }
129:
130: public EnableCheck createWindowWithLayerNamePanelMustBeActiveCheck() {
131: return new EnableCheck() {
132: public String check(JComponent component) {
133: return (!(workbenchContext.getWorkbench().getFrame()
134: .getActiveInternalFrame() instanceof LayerNamePanelProxy)) ? "A window with a layer-name panel must be active"
135: : null;
136: }
137: };
138: }
139:
140: public EnableCheck createWindowWithLayerViewPanelMustBeActiveCheck() {
141: return new EnableCheck() {
142: public String check(JComponent component) {
143: return (!(workbenchContext.getWorkbench().getFrame()
144: .getActiveInternalFrame() instanceof LayerViewPanelProxy)) ? "A window with a layer-view panel must be active"
145: : null;
146: }
147: };
148: }
149:
150: public EnableCheck createOnlyOneLayerMayHaveSelectedFeaturesCheck() {
151: return new EnableCheck() {
152: public String check(JComponent component) {
153: Collection layersWithSelectedFeatures = ((SelectionManagerProxy) workbenchContext
154: .getWorkbench().getFrame()
155: .getActiveInternalFrame())
156: .getSelectionManager().getFeatureSelection()
157: .getLayersWithSelectedItems();
158:
159: return (layersWithSelectedFeatures.size() > 1) ? "Only one layer may have selected features"
160: : null;
161: }
162: };
163: }
164:
165: public EnableCheck createOnlyOneLayerMayHaveSelectedItemsCheck() {
166: return new EnableCheck() {
167: public String check(JComponent component) {
168: Collection layersWithSelectedItems = ((SelectionManagerProxy) workbenchContext
169: .getWorkbench().getFrame()
170: .getActiveInternalFrame())
171: .getSelectionManager()
172: .getLayersWithSelectedItems();
173: return (layersWithSelectedItems.size() > 1) ? "Only one layer may have selected items"
174: : null;
175: }
176: };
177: }
178:
179: public EnableCheck createSelectedItemsLayersMustBeEditableCheck() {
180: return new EnableCheck() {
181: public String check(JComponent component) {
182: for (Iterator i = ((SelectionManagerProxy) workbenchContext
183: .getWorkbench().getFrame()
184: .getActiveInternalFrame())
185: .getSelectionManager()
186: .getLayersWithSelectedItems().iterator(); i
187: .hasNext();) {
188: Layer layer = (Layer) i.next();
189:
190: if (!layer.isEditable()) {
191: return "Selected items' layers must be editable ("
192: + layer.getName() + ")";
193: }
194: }
195:
196: return null;
197: }
198: };
199: }
200:
201: public EnableCheck createExactlyNCategoriesMustBeSelectedCheck(
202: final int n) {
203: return new EnableCheck() {
204: public String check(JComponent component) {
205: return (n != workbenchContext.getLayerNamePanel()
206: .getSelectedCategories().size()) ? ("Exactly "
207: + n + " categor" + StringUtil.ies(n) + " must be selected")
208: : null;
209: }
210: };
211: }
212:
213: public EnableCheck createExactlyNLayerablesMustBeSelectedCheck(
214: final int n, final Class layerableClass) {
215: return new EnableCheck() {
216: public String check(JComponent component) {
217: return (n != (workbenchContext.getLayerNamePanel())
218: .selectedNodes(layerableClass).size()) ? ("Exactly "
219: + n + " layer" + StringUtil.s(n) + " must be selected")
220: : null;
221: }
222: };
223: }
224:
225: public EnableCheck createExactlyNLayersMustBeSelectedCheck(
226: final int n) {
227: return createExactlyNLayerablesMustBeSelectedCheck(n,
228: Layer.class);
229: }
230:
231: public EnableCheck createAtLeastNCategoriesMustBeSelectedCheck(
232: final int n) {
233: return new EnableCheck() {
234: public String check(JComponent component) {
235: return (n > workbenchContext.getLayerNamePanel()
236: .getSelectedCategories().size()) ? ("At least "
237: + n + " categor" + StringUtil.ies(n) + " must be selected")
238: : null;
239: }
240: };
241: }
242:
243: public EnableCheck createAtLeastNLayerablesMustBeSelectedCheck(
244: final int n, final Class layerableClass) {
245: return new EnableCheck() {
246: public String check(JComponent component) {
247: LayerNamePanel layerNamePanel = workbenchContext
248: .getLayerNamePanel();
249: return (layerNamePanel == null || n > (workbenchContext
250: .getLayerNamePanel()).selectedNodes(
251: layerableClass).size()) ? ("At least " + n
252: + " layer" + StringUtil.s(n) + " must be selected")
253: : null;
254: }
255: };
256: }
257:
258: public EnableCheck createAtLeastNLayersMustBeSelectedCheck(
259: final int n) {
260: return createAtLeastNLayerablesMustBeSelectedCheck(n,
261: Layer.class);
262: }
263:
264: public EnableCheck createAtLeastNLayersMustBeEditableCheck(
265: final int n) {
266: return new EnableCheck() {
267: public String check(JComponent component) {
268: return (n > workbenchContext.getLayerManager()
269: .getEditableLayers().size()) ? ("At least " + n
270: + " layer" + StringUtil.s(n) + " must be editable")
271: : null;
272: }
273: };
274: }
275:
276: public EnableCheck createAtLeastNLayersMustExistCheck(final int n) {
277: return new EnableCheck() {
278: public String check(JComponent component) {
279: LayerManager layerManager = workbenchContext
280: .getLayerManager();
281: return (layerManager == null || n > layerManager.size()) ? ("At least "
282: + n + " layer" + StringUtil.s(n) + " must exist")
283: : null;
284: }
285: };
286: }
287:
288: public EnableCheck createAtMostNLayersMustExistCheck(final int n) {
289: return new EnableCheck() {
290: public String check(JComponent component) {
291: return (n < workbenchContext.getLayerManager().size()) ? ("At most "
292: + n + " layer" + StringUtil.s(n) + " must exist")
293: : null;
294: }
295: };
296: }
297:
298: public EnableCheck createExactlyNVectorsMustBeDrawnCheck(final int n) {
299: return new EnableCheck() {
300: public String check(JComponent component) {
301: return (n != vectorCount()) ? ("Exactly " + n
302: + " vector" + StringUtil.s(n) + " must be drawn")
303: : null;
304: }
305: };
306: }
307:
308: //<<TODO:REFACTORING>> I wonder if we can refactor some of these methods [Jon Aquino]
309: public EnableCheck createAtLeastNVectorsMustBeDrawnCheck(final int n) {
310: return new EnableCheck() {
311: public String check(JComponent component) {
312: return (n > vectorCount()) ? ("At least " + n
313: + " vector" + StringUtil.s(n) + " must be drawn")
314: : null;
315: }
316: };
317: }
318:
319: public EnableCheck createAtLeastNFeaturesMustBeSelectedCheck(
320: final int n) {
321: return new EnableCheck() {
322: public String check(JComponent component) {
323: return (n > ((SelectionManagerProxy) workbenchContext
324: .getWorkbench().getFrame()
325: .getActiveInternalFrame())
326: .getSelectionManager().getFeatureSelection()
327: .getFeaturesWithSelectedItems().size()) ? ("At least "
328: + n + " feature" + StringUtil.s(n) + " must be selected")
329: : null;
330: }
331: };
332: }
333:
334: public EnableCheck createAtLeastNItemsMustBeSelectedCheck(
335: final int n) {
336: return new EnableCheck() {
337: public String check(JComponent component) {
338: JInternalFrame iFrame = workbenchContext.getWorkbench()
339: .getFrame().getActiveInternalFrame();
340: int selected = 0;
341: try {//sstein [13. Aug. 2006]:
342: selected = ((SelectionManagerProxy) iFrame)
343: .getSelectionManager().getSelectedItems()
344: .size();
345: } catch (Exception e) {
346: //-- sstein:
347: //== eat exception ==
348: System.out
349: .println("eat exception @ EnableCheckFactory.createAtLeastNItemsMustBeSelectedCheck(i) if a non taskframe(or child) is selected");
350: //necessary if iFrame is OutputFrame or something
351: //and i dont know how to test for alle iFrames which exist or rather i do not know
352: //which are the ones accessible to the SelectionManager
353: }
354: String retVal = null;
355: if ((iFrame == null) || (n > selected)) {
356: retVal = ("At least " + n + " item"
357: + StringUtil.s(n) + " must be selected");
358: } else {
359: retVal = null;
360: }
361: return retVal;
362: }
363: };
364: }
365:
366: public EnableCheck createExactlyNFeaturesMustBeSelectedCheck(
367: final int n) {
368: return new EnableCheck() {
369: public String check(JComponent component) {
370: return (n != ((SelectionManagerProxy) workbenchContext
371: .getWorkbench().getFrame()
372: .getActiveInternalFrame())
373: .getSelectionManager().getFeatureSelection()
374: .getFeaturesWithSelectedItems().size()) ? ("Exactly "
375: + n + " feature" + StringUtil.s(n) + " must be selected")
376: : null;
377: }
378: };
379: }
380:
381: public EnableCheck createExactlyNItemsMustBeSelectedCheck(
382: final int n) {
383: return new EnableCheck() {
384: public String check(JComponent component) {
385: return (n != ((SelectionManagerProxy) workbenchContext
386: .getWorkbench().getFrame()
387: .getActiveInternalFrame())
388: .getSelectionManager().getSelectedItems()
389: .size()) ? ("Exactly " + n + " item"
390: + StringUtil.s(n) + " must be selected") : null;
391: }
392: };
393: }
394:
395: public EnableCheck createExactlyNLayersMustHaveSelectedItemsCheck(
396: final int n) {
397: return new EnableCheck() {
398: public String check(JComponent component) {
399: return (n != ((SelectionManagerProxy) workbenchContext
400: .getWorkbench().getFrame()
401: .getActiveInternalFrame())
402: .getSelectionManager()
403: .getLayersWithSelectedItems().size()) ? ("Exactly "
404: + n + " layer" + StringUtil.s(n) + " must have selected items")
405: : null;
406: }
407: };
408: }
409:
410: public EnableCheck createExactlyNFeaturesMustHaveSelectedItemsCheck(
411: final int n) {
412: return new EnableCheck() {
413: public String check(JComponent component) {
414: return (n != ((SelectionManagerProxy) workbenchContext
415: .getWorkbench().getFrame()
416: .getActiveInternalFrame())
417: .getSelectionManager()
418: .getFeaturesWithSelectedItems().size()) ? ("Exactly "
419: + n + " feature" + StringUtil.s(n) + " must have selected items")
420: : null;
421: }
422: };
423: }
424:
425: public EnableCheck createSelectedLayersMustBeEditableCheck() {
426: return new EnableCheck() {
427: public String check(JComponent component) {
428: for (Iterator i = Arrays.asList(
429: workbenchContext.getLayerNamePanel()
430: .getSelectedLayers()).iterator(); i
431: .hasNext();) {
432: Layer layer = (Layer) i.next();
433: if (!layer.isEditable()) {
434: return "Selected layers must be editable ("
435: + layer.getName() + ")";
436: }
437: }
438: return null;
439: }
440: };
441: }
442:
443: public EnableCheck createFenceMustBeDrawnCheck() {
444: return new EnableCheck() {
445: public String check(JComponent component) {
446: LayerViewPanel layerViewPanel = workbenchContext
447: .getLayerViewPanel();
448: return (layerViewPanel == null || //[UT] 20.10.2005 not quite the error mesg
449: null == layerViewPanel.getFence()) ? "A fence must be drawn"
450: : null;
451: }
452: };
453: }
454:
455: public EnableCheck createBetweenNAndMVectorsMustBeDrawnCheck(
456: final int min, final int max) {
457: return new EnableCheck() {
458: public String check(JComponent component) {
459: return ((vectorCount() > max) || (vectorCount() < min)) ? ("Between "
460: + min + " and " + max + " vectors must be drawn")
461: : null;
462: }
463: };
464: }
465:
466: private int vectorCount() {
467: return new WarpingVectorLayerFinder(workbenchContext)
468: .getVectors().size();
469: }
470:
471: public EnableCheck createAtLeastNFeaturesMustHaveSelectedItemsCheck(
472: final int n) {
473: return new EnableCheck() {
474: public String check(JComponent component) {
475: return (n > ((SelectionManagerProxy) workbenchContext
476: .getWorkbench().getFrame()
477: .getActiveInternalFrame())
478: .getSelectionManager()
479: .getFeaturesWithSelectedItems().size()) ? ("At least "
480: + n + " feature" + StringUtil.s(n) + " must have selected items")
481: : null;
482: }
483: };
484: }
485: }
|