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.ui;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Component;
037: import java.awt.Dimension;
038: import java.awt.Frame;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041: import java.awt.event.ComponentAdapter;
042: import java.awt.event.ComponentEvent;
043: import java.util.ArrayList;
044: import java.util.Collection;
045: import java.util.Iterator;
046:
047: import javax.swing.Icon;
048: import javax.swing.JDialog;
049: import javax.swing.JOptionPane;
050: import javax.swing.JPanel;
051: import javax.swing.JTabbedPane;
052:
053: import com.vividsolutions.jts.util.Assert;
054: import com.vividsolutions.jump.I18N;
055: import com.vividsolutions.jump.util.Blackboard;
056: import com.vividsolutions.jump.workbench.JUMPWorkbench;
057:
058: public class OptionsDialog extends JDialog {
059: private JPanel panel1 = new JPanel();
060: private BorderLayout borderLayout1 = new BorderLayout();
061: private OKCancelPanel okCancelPanel = new OKCancelPanel();
062: private JTabbedPane tabbedPane = new JTabbedPane();
063:
064: private static String sOptions = I18N
065: .get("com.vividsolutions.jump.workbench.ui.plugin.OptionsPlugIn");
066:
067: private OptionsDialog(Frame frame, String title, boolean modal) {
068: super (frame, title, modal);
069: try {
070: jbInit();
071: } catch (Exception ex) {
072: Assert.shouldNeverReachHere(ex.getMessage());
073: }
074:
075: addComponentListener(new ComponentAdapter() {
076: public void componentShown(ComponentEvent e) {
077: fireInit();
078: }
079: });
080:
081: okCancelPanel.addActionListener(new ActionListener() {
082: public void actionPerformed(ActionEvent e) {
083: if (okCancelPanel.wasOKPressed()) {
084: String errorMessage = validateInput();
085:
086: if (errorMessage != null) {
087: JOptionPane.showMessageDialog(
088: OptionsDialog.this , errorMessage,
089: "JUMP", JOptionPane.ERROR_MESSAGE);
090: return;
091: } else {
092: fireOKPressed();
093: setVisible(false);
094: return;
095: }
096: }
097: setVisible(false);
098: }
099: });
100: }
101:
102: public static OptionsDialog instance(JUMPWorkbench workbench) {
103: return instance(workbench.getBlackboard(), workbench.getFrame());
104: }
105:
106: public static OptionsDialog instance(Blackboard blackboard,
107: Frame frame) {
108: if (blackboard.get(OptionsDialog.class + " - INSTANCE") == null) {
109: return (OptionsDialog) blackboard.get(OptionsDialog.class
110: + " - INSTANCE", new OptionsDialog(frame, sOptions,
111: true));
112: }
113: return (OptionsDialog) blackboard.get(OptionsDialog.class
114: + " - INSTANCE");
115: }
116:
117: private void fireOKPressed() {
118: for (Iterator i = optionsPanels().iterator(); i.hasNext();) {
119: OptionsPanel panel = (OptionsPanel) i.next();
120: panel.okPressed();
121: }
122: }
123:
124: private void fireInit() {
125: for (Iterator i = optionsPanels().iterator(); i.hasNext();) {
126: OptionsPanel panel = (OptionsPanel) i.next();
127: panel.init();
128: }
129: }
130:
131: private Collection optionsPanels() {
132: ArrayList optionsPanels = new ArrayList();
133:
134: for (int i = 0; i < tabbedPane.getTabCount(); i++) {
135: optionsPanels.add(tabbedPane.getComponentAt(i));
136: }
137:
138: return optionsPanels;
139: }
140:
141: private String validateInput() {
142: for (Iterator i = optionsPanels().iterator(); i.hasNext();) {
143: OptionsPanel panel = (OptionsPanel) i.next();
144: String errorMessage = panel.validateInput();
145:
146: if (errorMessage != null) {
147: return errorMessage;
148: }
149: }
150:
151: return null;
152: }
153:
154: public void addTab(String title, OptionsPanel panel) {
155: addTab(title, null, panel);
156: }
157:
158: public void addTab(String title, Icon icon, OptionsPanel panel) {
159: tabbedPane.addTab(title, icon, (Component) panel);
160: pack();
161: }
162:
163: private void jbInit() throws Exception {
164: //Ensure dialog is wide enough to prevent a second row of tabs [Jon Aquino 11/6/2003]
165: JPanel strut = new JPanel();
166: strut.setPreferredSize(new Dimension(280, 0));
167: getContentPane().add(strut, BorderLayout.NORTH);
168: panel1.setLayout(borderLayout1);
169: this .setModal(true);
170: this .setTitle(sOptions);
171: getContentPane().add(panel1);
172: panel1.add(tabbedPane, BorderLayout.CENTER);
173: this .getContentPane().add(okCancelPanel, BorderLayout.SOUTH);
174: }
175:
176: public boolean wasOKPressed() {
177: return okCancelPanel.wasOKPressed();
178: }
179:
180: /**
181: * @return Returns the tabbedPane.
182: */
183: public JTabbedPane getTabbedPane() {
184: return tabbedPane;
185: }
186: }
|