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: package com.vividsolutions.jump.workbench.ui.style;
033:
034: import com.vividsolutions.jts.util.Assert;
035:
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.workbench.WorkbenchContext;
038: import com.vividsolutions.jump.workbench.model.Layer;
039: import com.vividsolutions.jump.workbench.model.UndoableCommand;
040: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
041: import com.vividsolutions.jump.workbench.plugin.EnableCheck;
042: import com.vividsolutions.jump.workbench.plugin.EnableCheckFactory;
043: import com.vividsolutions.jump.workbench.plugin.MultiEnableCheck;
044: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
045: import com.vividsolutions.jump.workbench.ui.GUIUtil;
046: import com.vividsolutions.jump.workbench.ui.MultiInputDialog;
047: import com.vividsolutions.jump.workbench.ui.images.IconLoader;
048: import com.vividsolutions.jump.workbench.ui.renderer.style.ColorThemingStylePanel;
049:
050: import java.awt.BorderLayout;
051: import java.awt.Component;
052: import java.awt.Dimension;
053: import java.awt.GridBagLayout;
054:
055: import java.util.ArrayList;
056: import java.util.Arrays;
057: import java.util.Collection;
058: import java.util.Iterator;
059:
060: import javax.swing.ImageIcon;
061: import javax.swing.JComboBox;
062: import javax.swing.JComponent;
063: import javax.swing.JFrame;
064: import javax.swing.JLabel;
065: import javax.swing.JPanel;
066: import javax.swing.JTabbedPane;
067:
068: public class ChangeStylesPlugIn extends AbstractPlugIn {
069: private final static String LAST_TAB_KEY = ChangeStylesPlugIn.class
070: .getName()
071: + " - LAST TAB";
072:
073: public ChangeStylesPlugIn() {
074: }
075:
076: public boolean execute(PlugInContext context) throws Exception {
077: final Layer layer = context.getSelectedLayer(0);
078: MultiInputDialog dialog = new MultiInputDialog(context
079: .getWorkbenchFrame(), I18N
080: .get("ui.style.ChangeStylesPlugIn.change-styles"), true);
081: dialog.setInset(0);
082: dialog.setSideBarImage(IconLoader.icon("Symbology.gif"));
083: dialog
084: .setSideBarDescription(I18N
085: .get("ui.style.ChangeStylesPlugIn.you-can-use-this-dialog-to-change-the-colour-line-width"));
086:
087: final ArrayList stylePanels = new ArrayList();
088: RenderingStylePanel renderingStylePanel = new RenderingStylePanel(
089: context.getWorkbenchContext().getWorkbench()
090: .getBlackboard(), layer);
091: stylePanels.add(renderingStylePanel);
092: stylePanels.add(new ScaleStylePanel(layer, context
093: .getLayerViewPanel()));
094:
095: //Only set preferred size for DecorationStylePanel or ColorThemingStylePanel;
096: //otherwise they will grow to the height of the screen. But don't set
097: //the preferred size of LabelStylePanel to (400, 300) -- in fact, it needs
098: //a bit more height -- any less, and its text boxes will shrink to
099: //zero-width. I've found that if you don't give text boxes enough height,
100: //they simply shrink to zero-width. [Jon Aquino]
101: DecorationStylePanel decorationStylePanel = new DecorationStylePanel(
102: layer, context.getWorkbenchFrame()
103: .getChoosableStyleClasses());
104: decorationStylePanel.setPreferredSize(new Dimension(400, 300));
105:
106: if (layer.getFeatureCollectionWrapper().getFeatureSchema()
107: .getAttributeCount() > 1) {
108: ColorThemingStylePanel colorThemingStylePanel = new ColorThemingStylePanel(
109: layer, context.getWorkbenchContext());
110: colorThemingStylePanel.setPreferredSize(new Dimension(400,
111: 300));
112: stylePanels.add(colorThemingStylePanel);
113: GUIUtil.sync(renderingStylePanel.getTransparencySlider(),
114: colorThemingStylePanel.getTransparencySlider());
115: GUIUtil.sync(renderingStylePanel.getSynchronizeCheckBox(),
116: colorThemingStylePanel.getSynchronizeCheckBox());
117: } else {
118: stylePanels.add(new DummyColorThemingStylePanel());
119: }
120:
121: stylePanels
122: .add(new LabelStylePanel(layer, context
123: .getLayerViewPanel(), dialog, context
124: .getErrorHandler()));
125: stylePanels.add(decorationStylePanel);
126:
127: JTabbedPane tabbedPane = new JTabbedPane();
128:
129: for (Iterator i = stylePanels.iterator(); i.hasNext();) {
130: final StylePanel stylePanel = (StylePanel) i.next();
131: tabbedPane.add((Component) stylePanel, stylePanel
132: .getTitle());
133: dialog.addEnableChecks(stylePanel.getTitle(), Arrays
134: .asList(new EnableCheck[] { new EnableCheck() {
135: public String check(JComponent component) {
136: return stylePanel.validateInput();
137: }
138: } }));
139: }
140:
141: dialog.addRow(tabbedPane);
142: tabbedPane.setSelectedComponent(find(stylePanels,
143: (String) context.getWorkbenchContext().getWorkbench()
144: .getBlackboard().get(
145: LAST_TAB_KEY,
146: ((StylePanel) stylePanels.iterator()
147: .next()).getTitle())));
148: dialog.setVisible(true);
149: context
150: .getWorkbenchContext()
151: .getWorkbench()
152: .getBlackboard()
153: .put(
154: LAST_TAB_KEY,
155: ((StylePanel) tabbedPane.getSelectedComponent())
156: .getTitle());
157:
158: if (dialog.wasOKPressed()) {
159: final Collection oldStyles = layer.cloneStyles();
160: layer.getLayerManager().deferFiringEvents(new Runnable() {
161: public void run() {
162: for (Iterator i = stylePanels.iterator(); i
163: .hasNext();) {
164: StylePanel stylePanel = (StylePanel) i.next();
165: stylePanel.updateStyles();
166: }
167: }
168: });
169:
170: final Collection newStyles = layer.cloneStyles();
171: execute(new UndoableCommand(getName()) {
172: public void execute() {
173: layer.setStyles(newStyles);
174: }
175:
176: public void unexecute() {
177: layer.setStyles(oldStyles);
178: }
179: }, context);
180: return true;
181: }
182:
183: return false;
184: }
185:
186: private Component find(Collection stylePanels, String title) {
187: for (Iterator i = stylePanels.iterator(); i.hasNext();) {
188: StylePanel stylePanel = (StylePanel) i.next();
189:
190: if (stylePanel.getTitle().equals(title)) {
191: return (Component) stylePanel;
192: }
193: }
194:
195: Assert.shouldNeverReachHere();
196:
197: return null;
198: }
199:
200: public ImageIcon getIcon() {
201: return IconLoader.icon("Palette.gif");
202: }
203:
204: public MultiEnableCheck createEnableCheck(
205: final WorkbenchContext workbenchContext) {
206: EnableCheckFactory checkFactory = new EnableCheckFactory(
207: workbenchContext);
208:
209: return new MultiEnableCheck()
210: .add(
211: checkFactory
212: .createWindowWithLayerNamePanelMustBeActiveCheck())
213: // ScaledStylePanel assumes that the active window has a LayerViewPanel. [Jon Aquino 2005-08-09]
214: .add(
215: checkFactory
216: .createWindowWithLayerViewPanelMustBeActiveCheck())
217: .add(
218: checkFactory
219: .createExactlyNLayersMustBeSelectedCheck(1));
220: }
221:
222: private class DummyColorThemingStylePanel extends JPanel implements
223: StylePanel {
224: public DummyColorThemingStylePanel() {
225: //GridBagLayout so it gets centered. [Jon Aquino]
226: super (new GridBagLayout());
227: add(new JLabel(
228: I18N
229: .get("ui.style.ChangeStylesPlugIn.this-layer-has-no-attributes")));
230: }
231:
232: public String getTitle() {
233: return ColorThemingStylePanel.TITLE;
234: }
235:
236: public void updateStyles() {
237: }
238:
239: public String validateInput() {
240: return null;
241: }
242: }
243: }
|