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.snap;
033:
034: import com.vividsolutions.jts.util.Assert;
035:
036: import com.vividsolutions.jump.I18N;
037: import com.vividsolutions.jump.util.Blackboard;
038: import com.vividsolutions.jump.workbench.ui.OptionsPanel;
039: import com.vividsolutions.jump.workbench.ui.ValidatingTextField;
040:
041: import java.awt.*;
042:
043: import javax.swing.*;
044: import javax.swing.border.Border;
045: import javax.swing.border.TitledBorder;
046: import java.awt.event.*;
047:
048: public class SnapOptionsPanel extends JPanel implements OptionsPanel {
049: private BorderLayout borderLayout1 = new BorderLayout();
050: private JPanel jPanel1 = new JPanel();
051: private GridBagLayout gridBagLayout1 = new GridBagLayout();
052: private JPanel jPanel2 = new JPanel();
053: private GridBagLayout gridBagLayout2 = new GridBagLayout();
054: private TitledBorder titledBorder1;
055: private JCheckBox snapToFeaturesCheckBox = new JCheckBox();
056: private JCheckBox snapToVerticesCheckBox = new JCheckBox();
057: private JPanel jPanel3 = new JPanel();
058: private JPanel jPanel4 = new JPanel();
059: private JCheckBox snapToGridCheckBox = new JCheckBox();
060: private GridBagLayout gridBagLayout3 = new GridBagLayout();
061: private JPanel jPanel5 = new JPanel();
062: private Border border2;
063: private TitledBorder titledBorder2;
064: private GridBagLayout gridBagLayout4 = new GridBagLayout();
065: private GridBagLayout gridBagLayout6 = new GridBagLayout();
066: private JTextField gridSizeTextField = new JTextField(7);
067: private JPanel jPanel7 = new JPanel();
068: private JRadioButton showGridDotsRadioButton = new JRadioButton();
069: private JRadioButton showGridLinesRadioButton = new JRadioButton();
070: private JPanel jPanel6 = new JPanel();
071: private Blackboard blackboard;
072: private ButtonGroup buttonGroup = new ButtonGroup();
073: private ValidatingTextField toleranceTextField = new ValidatingTextField(
074: "", 3, SwingConstants.RIGHT,
075: new ValidatingTextField.BoundedIntValidator(0, 100),
076: ValidatingTextField.DUMMY_CLEANER);
077: private JCheckBox showGridCheckBox = new JCheckBox();
078: private JPanel tolerancePanel = new JPanel();
079: private GridBagLayout gridBagLayout5 = new GridBagLayout();
080: private JLabel toleranceLabel = new JLabel();
081: private JLabel toleranceUnitsLabel = new JLabel();
082: private JLabel showGridUnitsLabel = new JLabel();
083:
084: public SnapOptionsPanel(Blackboard blackboard) {
085: this .blackboard = blackboard;
086:
087: try {
088: jbInit();
089: } catch (Exception e) {
090: Assert.shouldNeverReachHere(e.toString());
091: }
092:
093: buttonGroup.add(showGridDotsRadioButton);
094: buttonGroup.add(showGridLinesRadioButton);
095: snapToFeaturesCheckBox.addActionListener(new ActionListener() {
096: public void actionPerformed(ActionEvent e) {
097: updateEnabled();
098: }
099: });
100: }
101:
102: private void updateEnabled() {
103: snapToVerticesCheckBox.setEnabled(!snapToFeaturesCheckBox
104: .isSelected());
105: gridSizeTextField.setEnabled(showGridCheckBox.isSelected());
106: showGridDotsRadioButton.setEnabled(showGridCheckBox
107: .isSelected());
108: showGridLinesRadioButton.setEnabled(showGridCheckBox
109: .isSelected());
110: }
111:
112: public String validateInput() {
113: String errorMessage = "\""
114: + gridSizeTextField.getText()
115: + "\" "
116: + I18N
117: .get("ui.snap.SnapOptionsPanel.is-not-a-valid-grid-size");
118:
119: try {
120: if (Double.parseDouble(gridSizeTextField.getText()) <= 0) {
121: return errorMessage;
122: }
123: } catch (NumberFormatException e) {
124: return errorMessage;
125: }
126:
127: return null;
128: }
129:
130: public void okPressed() {
131: blackboard.put(SnapToVerticesPolicy.ENABLED_KEY,
132: snapToVerticesCheckBox.isSelected());
133: SnapManager.setToleranceInPixels(toleranceTextField
134: .getInteger(), blackboard);
135: blackboard.put(SnapToFeaturesPolicy.ENABLED_KEY,
136: snapToFeaturesCheckBox.isSelected());
137: blackboard.put(SnapToGridPolicy.ENABLED_KEY, snapToGridCheckBox
138: .isSelected());
139: blackboard.put(SnapToGridPolicy.GRID_SIZE_KEY, Double
140: .parseDouble(gridSizeTextField.getText()));
141: blackboard.put(GridRenderer.DOTS_ENABLED_KEY,
142: showGridDotsRadioButton.isSelected());
143: blackboard.put(GridRenderer.LINES_ENABLED_KEY,
144: showGridLinesRadioButton.isSelected());
145: blackboard.put(GridRenderer.ENABLED_KEY, showGridCheckBox
146: .isSelected());
147: }
148:
149: public void init() {
150: snapToVerticesCheckBox.setSelected(blackboard.get(
151: SnapToVerticesPolicy.ENABLED_KEY, false));
152: toleranceTextField.setText(""
153: + SnapManager.getToleranceInPixels(blackboard));
154: snapToFeaturesCheckBox.setSelected(blackboard.get(
155: SnapToFeaturesPolicy.ENABLED_KEY, false));
156: snapToGridCheckBox.setSelected(blackboard.get(
157: SnapToGridPolicy.ENABLED_KEY, false));
158: gridSizeTextField.setText(""
159: + blackboard.get(SnapToGridPolicy.GRID_SIZE_KEY, 20d));
160: showGridCheckBox.setSelected(blackboard.get(
161: GridRenderer.ENABLED_KEY, false));
162: showGridDotsRadioButton.setSelected(blackboard.get(
163: GridRenderer.DOTS_ENABLED_KEY, false));
164: showGridLinesRadioButton.setSelected(blackboard.get(
165: GridRenderer.LINES_ENABLED_KEY, true));
166: updateEnabled();
167: }
168:
169: private void jbInit() throws Exception {
170: titledBorder1 = new TitledBorder(BorderFactory
171: .createEtchedBorder(Color.white, new Color(148, 145,
172: 140)), I18N
173: .get("ui.snap.SnapOptionsPanel.snapping"));
174: border2 = BorderFactory.createEtchedBorder(Color.white,
175: new Color(148, 145, 140));
176: titledBorder2 = new TitledBorder(border2, I18N
177: .get("ui.snap.SnapOptionsPanel.grid-display"));
178: this .setLayout(borderLayout1);
179: jPanel1.setLayout(gridBagLayout1);
180: jPanel2.setLayout(gridBagLayout2);
181: jPanel2.setBorder(titledBorder1);
182: snapToFeaturesCheckBox
183: .setText(I18N
184: .get("ui.snap.SnapOptionsPanel.snap-to-vertices-and-lines"));
185: snapToVerticesCheckBox.setText(I18N
186: .get("ui.snap.SnapOptionsPanel.snap-to-vertices"));
187: snapToGridCheckBox.setText(I18N
188: .get("ui.snap.SnapOptionsPanel.snap-to-grid"));
189: jPanel4.setLayout(gridBagLayout3);
190: jPanel5.setBorder(titledBorder2);
191: jPanel5.setLayout(gridBagLayout4);
192: gridSizeTextField.setText("20");
193: gridSizeTextField
194: .setHorizontalAlignment(SwingConstants.TRAILING);
195: jPanel7.setLayout(gridBagLayout6);
196: showGridDotsRadioButton.setSelected(true);
197: showGridDotsRadioButton.setText(I18N
198: .get("ui.snap.SnapOptionsPanel.show-grid-as-dots"));
199: showGridLinesRadioButton.setText(I18N
200: .get("ui.snap.SnapOptionsPanel.show-grid-as-lines"));
201: showGridCheckBox.setToolTipText("");
202: showGridCheckBox.setText(I18N
203: .get("ui.snap.SnapOptionsPanel.show-grid-size"));
204: showGridCheckBox
205: .addActionListener(new java.awt.event.ActionListener() {
206: public void actionPerformed(ActionEvent e) {
207: showGridCheckBox_actionPerformed(e);
208: }
209: });
210: tolerancePanel.setLayout(gridBagLayout5);
211: toleranceLabel.setText(I18N
212: .get("ui.snap.SnapOptionsPanel.tolerance"));
213: toleranceUnitsLabel.setText(I18N
214: .get("ui.snap.SnapOptionsPanel.pixels"));
215: showGridUnitsLabel.setText(I18N
216: .get("ui.snap.SnapOptionsPanel.model-units"));
217: this .add(jPanel1, BorderLayout.CENTER);
218: jPanel1.add(jPanel2, new GridBagConstraints(0, 0, 1, 1, 1.0,
219: 0.0, GridBagConstraints.CENTER,
220: GridBagConstraints.HORIZONTAL, new Insets(10, 10, 10,
221: 10), 0, 0));
222: jPanel2.add(snapToFeaturesCheckBox, new GridBagConstraints(0,
223: 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
224: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
225: jPanel2.add(snapToVerticesCheckBox, new GridBagConstraints(0,
226: 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
227: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
228: jPanel2.add(jPanel3, new GridBagConstraints(1, 1, 1, 1, 1.0,
229: 0.0, GridBagConstraints.CENTER,
230: GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),
231: 0, 0));
232: jPanel2.add(jPanel4, new GridBagConstraints(0, 3, 1, 1, 0.0,
233: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
234: new Insets(0, 0, 0, 0), 0, 0));
235: jPanel4.add(snapToGridCheckBox, new GridBagConstraints(0, 0, 1,
236: 1, 0.0, 0.0, GridBagConstraints.WEST,
237: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
238: jPanel2.add(tolerancePanel, new GridBagConstraints(0, 0, 1, 1,
239: 0.0, 0.0, GridBagConstraints.WEST,
240: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
241: tolerancePanel.add(toleranceLabel, new GridBagConstraints(1, 0,
242: 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
243: GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
244: tolerancePanel.add(toleranceTextField, new GridBagConstraints(
245: 2, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
246: GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
247: tolerancePanel.add(toleranceUnitsLabel, new GridBagConstraints(
248: 3, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER,
249: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
250: jPanel1.add(jPanel5, new GridBagConstraints(0, 1, 1, 1, 1.0,
251: 0.0, GridBagConstraints.CENTER,
252: GridBagConstraints.HORIZONTAL,
253: new Insets(0, 10, 10, 10), 0, 0));
254: jPanel5.add(jPanel7, new GridBagConstraints(0, 1, 1, 1, 1.0,
255: 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE,
256: new Insets(0, 0, 0, 0), 0, 0));
257: jPanel7.add(gridSizeTextField, new GridBagConstraints(1, 0, 1,
258: 1, 0.0, 0.0, GridBagConstraints.CENTER,
259: GridBagConstraints.NONE, new Insets(0, 0, 0, 5), 0, 0));
260: jPanel7.add(showGridCheckBox, new GridBagConstraints(0, 0, 1,
261: 1, 0.0, 0.0, GridBagConstraints.CENTER,
262: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
263: jPanel5.add(showGridDotsRadioButton, new GridBagConstraints(0,
264: 2, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
265: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
266: jPanel5.add(showGridLinesRadioButton, new GridBagConstraints(0,
267: 3, 1, 1, 0.0, 0.0, GridBagConstraints.WEST,
268: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
269: jPanel1.add(jPanel6, new GridBagConstraints(0, 2, 1, 1, 0.0,
270: 1.0, GridBagConstraints.CENTER,
271: GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0,
272: 0));
273: jPanel7.add(showGridUnitsLabel, new GridBagConstraints(2, 0, 1,
274: 1, 0.0, 0.0, GridBagConstraints.CENTER,
275: GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
276: }
277:
278: void showGridCheckBox_actionPerformed(ActionEvent e) {
279: updateEnabled();
280: }
281: }
|