001: /*
002: * This file is part of the GeOxygene project source files.
003: *
004: * GeOxygene aims at providing an open framework which implements OGC/ISO specifications for
005: * the development and deployment of geographic (GIS) applications. It is a open source
006: * contribution of the COGIT laboratory at the Institut Géographique National (the French
007: * National Mapping Agency).
008: *
009: * See: http://oxygene-project.sourceforge.net
010: *
011: * Copyright (C) 2005 Institut Géographique National
012: *
013: * This library is free software; you can redistribute it and/or modify it under the terms
014: * of the GNU Lesser General Public License as published by the Free Software Foundation;
015: * either version 2.1 of the License, or any later version.
016: *
017: * This library is distributed in the hope that it will be useful, but WITHOUT ANY
018: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
019: * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser General Public License along with
022: * this library (see file LICENSE if present); if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: *
025: */
026:
027: package fr.ign.cogit.geoxygene.util.viewer;
028:
029: import java.awt.BorderLayout;
030: import java.awt.Container;
031: import java.awt.FlowLayout;
032: import java.awt.GridLayout;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.WindowAdapter;
036: import java.awt.event.WindowEvent;
037: import java.util.Vector;
038:
039: import javax.swing.BorderFactory;
040: import javax.swing.Icon;
041: import javax.swing.JButton;
042: import javax.swing.JComboBox;
043: import javax.swing.JFrame;
044: import javax.swing.JLabel;
045: import javax.swing.JPanel;
046: import javax.swing.JTabbedPane;
047:
048: import uk.ac.leeds.ccg.dbffile.Dbf;
049: import uk.ac.leeds.ccg.geotools.HSVShader;
050: import uk.ac.leeds.ccg.geotools.MonoShader;
051: import uk.ac.leeds.ccg.geotools.RandomShader;
052: import uk.ac.leeds.ccg.geotools.Shader;
053: import uk.ac.leeds.ccg.geotools.ShapefileReader;
054: import uk.ac.leeds.ccg.geotools.UniqueShader;
055:
056: /**
057: * This class implements the frame for the defintion of properties of themes to be displayed
058: * in the (Geo)Object viewer.
059: *
060: * @author Thierry Badard & Arnaud Braun
061: * @version 1.0
062: *
063: */
064:
065: class ObjectViewerThemePropertiesFrame extends JFrame {
066:
067: public static final String FRAME_TITLE = "GeOxygene Object Viewer - Theme Properties";
068:
069: private ObjectViewerThemeProperties objectViewerThemeProperties;
070:
071: public ObjectViewerThemePropertiesFrame(
072: ObjectViewerThemeProperties objectViewerThemeProperties) {
073:
074: super (FRAME_TITLE);
075:
076: this .objectViewerThemeProperties = objectViewerThemeProperties;
077:
078: //Set up the theme properties panel
079: JTabbedPane themePropertiesTabbedPanel = new JTabbedPane();
080:
081: JPanel themePropertiesPanel = new JPanel(new GridLayout(6, 2,
082: 10, 10));
083: themePropertiesPanel.setBorder(BorderFactory
084: .createTitledBorder("Theme Properties"));
085:
086: JPanel themeShadersPanel = new JPanel(new GridLayout(3, 2, 1,
087: 50));
088: themeShadersPanel.setBorder(BorderFactory
089: .createTitledBorder("Shader Properties"));
090:
091: //Add the components to the themePropertiesPanel
092: JLabel fillThemeColorLabel = new JLabel("Fill in Theme Color:");
093: Icon fillThemeColorButtonIcon = new RectIcon(
094: getObjectViewerThemeProperties().getFillInThemeColor());
095: final JButton fillThemeColorButton = new JButton(
096: fillThemeColorButtonIcon);
097: fillThemeColorButton.addActionListener(new ActionListener() {
098: public void actionPerformed(ActionEvent e) {
099: final JFrame ccframe = new ObjectViewerColorChooser(
100: ((RectIcon) (fillThemeColorButton.getIcon()))
101: .getColor(), fillThemeColorButton);
102: ccframe.addWindowListener(new WindowAdapter() {
103: public void windowClosing(WindowEvent e) {
104: ccframe.dispose();
105: }
106: });
107: ccframe.setResizable(false);
108: ccframe.pack();
109: ccframe.setVisible(true);
110: }
111: });
112:
113: JLabel outlineThemeColorLabel = new JLabel(
114: "Outline Theme Color:");
115: Icon outlineThemeColorButtonIcon = new RectIcon(
116: getObjectViewerThemeProperties().getOutlineThemeColor());
117: final JButton outlineThemeColorButton = new JButton(
118: outlineThemeColorButtonIcon);
119: outlineThemeColorButton.addActionListener(new ActionListener() {
120: public void actionPerformed(ActionEvent e) {
121: final JFrame ccframe = new ObjectViewerColorChooser(
122: ((RectIcon) (outlineThemeColorButton.getIcon()))
123: .getColor(), outlineThemeColorButton);
124: ccframe.addWindowListener(new WindowAdapter() {
125: public void windowClosing(WindowEvent e) {
126: ccframe.dispose();
127: }
128: });
129: ccframe.setResizable(false);
130: ccframe.pack();
131: ccframe.setVisible(true);
132: }
133: });
134:
135: JLabel fillHighlightColorLabel = new JLabel(
136: "Fill in Highlight Color:");
137: Icon fillHighlightColorButtonIcon = new RectIcon(
138: objectViewerThemeProperties.getFillInHighlightColor());
139: final JButton fillHighlightColorButton = new JButton(
140: fillHighlightColorButtonIcon);
141: fillHighlightColorButton
142: .addActionListener(new ActionListener() {
143: public void actionPerformed(ActionEvent e) {
144: final JFrame ccframe = new ObjectViewerColorChooser(
145: ((RectIcon) (fillHighlightColorButton
146: .getIcon())).getColor(),
147: fillHighlightColorButton);
148: ccframe.addWindowListener(new WindowAdapter() {
149: public void windowClosing(WindowEvent e) {
150: ccframe.dispose();
151: }
152: });
153: ccframe.setResizable(false);
154: ccframe.pack();
155: ccframe.setVisible(true);
156: }
157: });
158:
159: JLabel outlineHighlightColorLabel = new JLabel(
160: "Outline Highlight Color:");
161: Icon outlineHighlightColorButtonIcon = new RectIcon(
162: getObjectViewerThemeProperties()
163: .getOutlineHighlightColor());
164: final JButton outlineHighlightColorButton = new JButton(
165: outlineHighlightColorButtonIcon);
166: outlineHighlightColorButton
167: .addActionListener(new ActionListener() {
168: public void actionPerformed(ActionEvent e) {
169: final JFrame ccframe = new ObjectViewerColorChooser(
170: ((RectIcon) (outlineHighlightColorButton
171: .getIcon())).getColor(),
172: outlineHighlightColorButton);
173: ccframe.addWindowListener(new WindowAdapter() {
174: public void windowClosing(WindowEvent e) {
175: ccframe.dispose();
176: }
177: });
178: ccframe.setResizable(false);
179: ccframe.pack();
180: ccframe.setVisible(true);
181: }
182: });
183:
184: JLabel fillSelectionColorLabel = new JLabel(
185: "Fill in Selection Color:");
186: Icon fillSelectionColorButtonIcon = new RectIcon(
187: getObjectViewerThemeProperties()
188: .getFillInSelectionColor());
189: final JButton fillSelectionColorButton = new JButton(
190: fillSelectionColorButtonIcon);
191: fillSelectionColorButton
192: .addActionListener(new ActionListener() {
193: public void actionPerformed(ActionEvent e) {
194: final JFrame ccframe = new ObjectViewerColorChooser(
195: ((RectIcon) (fillSelectionColorButton
196: .getIcon())).getColor(),
197: fillSelectionColorButton);
198: ccframe.addWindowListener(new WindowAdapter() {
199: public void windowClosing(WindowEvent e) {
200: ccframe.dispose();
201: }
202: });
203: ccframe.setResizable(false);
204: ccframe.pack();
205: ccframe.setVisible(true);
206: }
207: });
208:
209: JLabel outlineSelectionColorLabel = new JLabel(
210: "Outline Selection Color:");
211: Icon outlineSelectionColorButtonIcon = new RectIcon(
212: getObjectViewerThemeProperties()
213: .getOutlineSelectionColor());
214: final JButton outlineSelectionColorButton = new JButton(
215: outlineSelectionColorButtonIcon);
216: outlineSelectionColorButton
217: .addActionListener(new ActionListener() {
218: public void actionPerformed(ActionEvent e) {
219: final JFrame ccframe = new ObjectViewerColorChooser(
220: ((RectIcon) (outlineSelectionColorButton
221: .getIcon())).getColor(),
222: outlineSelectionColorButton);
223: ccframe.addWindowListener(new WindowAdapter() {
224: public void windowClosing(WindowEvent e) {
225: ccframe.dispose();
226: }
227: });
228: ccframe.setResizable(false);
229: ccframe.pack();
230: ccframe.setVisible(true);
231: }
232: });
233:
234: themePropertiesPanel.add(fillThemeColorLabel);
235: themePropertiesPanel.add(fillThemeColorButton);
236: themePropertiesPanel.add(outlineThemeColorLabel);
237: themePropertiesPanel.add(outlineThemeColorButton);
238:
239: themePropertiesPanel.add(fillHighlightColorLabel);
240: themePropertiesPanel.add(fillHighlightColorButton);
241: themePropertiesPanel.add(outlineHighlightColorLabel);
242: themePropertiesPanel.add(outlineHighlightColorButton);
243:
244: themePropertiesPanel.add(fillSelectionColorLabel);
245: themePropertiesPanel.add(fillSelectionColorButton);
246: themePropertiesPanel.add(outlineSelectionColorLabel);
247: themePropertiesPanel.add(outlineSelectionColorButton);
248:
249: //Add the components to the themeShadersPanel
250: final JComboBox shaderTypeComboBox;
251: final JComboBox attributeDoubleComboBox;
252: final JComboBox attributeStringComboBox;
253:
254: if (getObjectViewerThemeProperties().getDataSourceType() == Utils.GEOXYGENE) {
255:
256: JLabel shaderLabel = new JLabel("Type of shader:");
257: shaderTypeComboBox = new JComboBox(new String[] { "Mono",
258: "Random", "Continuous", "Unique" });
259: shaderTypeComboBox
260: .setSelectedItem(getShaderName(getObjectViewerThemeProperties()
261: .getShader()));
262: final JLabel attributeDoubleLabel = new JLabel(
263: "Attributes (double or int):");
264: attributeDoubleComboBox = new JComboBox();
265: final JLabel attributeStringLabel = new JLabel(
266: "Attributes (String):");
267: attributeStringComboBox = new JComboBox();
268:
269: String[] doubleFieldsNames = getDoubleOrIntFields();
270: if (doubleFieldsNames.length > 0) {
271: for (int i = 0; i < doubleFieldsNames.length; i++)
272: attributeDoubleComboBox
273: .addItem(doubleFieldsNames[i]);
274: if (getObjectViewerThemeProperties().getShadedBy() != null
275: && getSelectedFieldClass(
276: getObjectViewerThemeProperties()
277: .getShadedBy()).equals(
278: double.class))
279: attributeDoubleComboBox
280: .setSelectedItem(getObjectViewerThemeProperties()
281: .getShadedBy());
282: else
283: attributeDoubleComboBox
284: .setSelectedItem(doubleFieldsNames[0]);
285: }
286:
287: String[] stringFieldsNames = getStringFields();
288: if (stringFieldsNames.length > 0) {
289: for (int i = 0; i < stringFieldsNames.length; i++)
290: attributeStringComboBox
291: .addItem(stringFieldsNames[i]);
292: if (getObjectViewerThemeProperties().getShadedBy() != null
293: && getSelectedFieldClass(
294: getObjectViewerThemeProperties()
295: .getShadedBy()).equals(
296: String.class))
297: attributeStringComboBox
298: .setSelectedItem(getObjectViewerThemeProperties()
299: .getShadedBy());
300: else
301: attributeStringComboBox
302: .setSelectedItem(stringFieldsNames[0]);
303: }
304:
305: themeShadersPanel.add(shaderLabel);
306: themeShadersPanel.add(shaderTypeComboBox);
307: themeShadersPanel.add(attributeDoubleLabel);
308: themeShadersPanel.add(attributeDoubleComboBox);
309: themeShadersPanel.add(attributeStringLabel);
310: themeShadersPanel.add(attributeStringComboBox);
311:
312: attributeDoubleLabel.setEnabled(false);
313: attributeDoubleComboBox.setEnabled(false);
314: attributeStringLabel.setEnabled(false);
315: attributeStringComboBox.setEnabled(false);
316:
317: shaderTypeComboBox.addActionListener(new ActionListener() {
318: public void actionPerformed(ActionEvent e) {
319: if (shaderTypeComboBox.getSelectedItem().equals(
320: "Continuous")) {
321: attributeDoubleLabel.setEnabled(true);
322: attributeDoubleComboBox.setEnabled(true);
323: attributeStringLabel.setEnabled(false);
324: attributeStringComboBox.setEnabled(false);
325: } else if (shaderTypeComboBox.getSelectedItem()
326: .equals("Unique")) {
327: attributeDoubleLabel.setEnabled(false);
328: attributeDoubleComboBox.setEnabled(false);
329: attributeStringLabel.setEnabled(true);
330: attributeStringComboBox.setEnabled(true);
331: } else {
332: attributeDoubleLabel.setEnabled(false);
333: attributeDoubleComboBox.setEnabled(false);
334: attributeStringLabel.setEnabled(false);
335: attributeStringComboBox.setEnabled(false);
336: }
337: }
338: });
339:
340: } else {
341: shaderTypeComboBox = null;
342: attributeDoubleComboBox = null;
343: attributeStringComboBox = null;
344: }
345:
346: //Add the control panel at the bottom of the window
347: final JPanel controlPanel = new JPanel(new FlowLayout(
348: FlowLayout.CENTER, 20, 10));
349: JButton okButton = new JButton("Ok");
350: okButton.addActionListener(new ActionListener() {
351: public void actionPerformed(ActionEvent e) {
352: ObjectViewerThemePropertiesFrame.this .dispose();
353: getObjectViewerThemeProperties().setFillInThemeColor(
354: ((RectIcon) (fillThemeColorButton.getIcon()))
355: .getColor());
356: getObjectViewerThemeProperties()
357: .setOutlineThemeColor(
358: ((RectIcon) (outlineThemeColorButton
359: .getIcon())).getColor());
360: getObjectViewerThemeProperties()
361: .setFillInHighlightColor(
362: ((RectIcon) (fillHighlightColorButton
363: .getIcon())).getColor());
364: getObjectViewerThemeProperties()
365: .setOutlineHighlightColor(
366: ((RectIcon) (outlineHighlightColorButton
367: .getIcon())).getColor());
368: getObjectViewerThemeProperties()
369: .setFillInSelectionColor(
370: ((RectIcon) (fillSelectionColorButton
371: .getIcon())).getColor());
372: getObjectViewerThemeProperties()
373: .setOutlineSelectionColor(
374: ((RectIcon) (outlineSelectionColorButton
375: .getIcon())).getColor());
376:
377: if (getObjectViewerThemeProperties()
378: .getDataSourceType() == Utils.GEOXYGENE) {
379:
380: if (attributeDoubleComboBox.isEnabled()
381: && !attributeStringComboBox.isEnabled())
382: getObjectViewerThemeProperties()
383: .setShader(
384: selectShader((String) shaderTypeComboBox
385: .getSelectedItem()),
386: (String) attributeDoubleComboBox
387: .getSelectedItem());
388: else if (!attributeDoubleComboBox.isEnabled()
389: && attributeStringComboBox.isEnabled())
390: getObjectViewerThemeProperties()
391: .setShader(
392: selectShader((String) shaderTypeComboBox
393: .getSelectedItem()),
394: (String) attributeStringComboBox
395: .getSelectedItem());
396: else
397: getObjectViewerThemeProperties()
398: .setShader(
399: selectShader((String) shaderTypeComboBox
400: .getSelectedItem()));
401:
402: }
403:
404: getObjectViewerThemeProperties().setChanged();
405: }
406: });
407: JButton cancelButton = new JButton("Cancel");
408: cancelButton.addActionListener(new ActionListener() {
409: public void actionPerformed(ActionEvent e) {
410: ObjectViewerThemePropertiesFrame.this .dispose();
411: }
412: });
413: controlPanel.add(okButton);
414: controlPanel.add(cancelButton);
415:
416: //Add the components to the colorchooser frame
417: Container contentPane = getContentPane();
418: themePropertiesTabbedPanel.addTab("Colors",
419: themePropertiesPanel);
420: themePropertiesTabbedPanel.setToolTipTextAt(0,
421: "Set the colors for the current theme.");
422: themePropertiesTabbedPanel.setSelectedIndex(0);
423: themePropertiesTabbedPanel.addTab("Shaders", themeShadersPanel);
424: themePropertiesTabbedPanel.setToolTipTextAt(1,
425: "Change the shader of the current theme.");
426:
427: contentPane
428: .add(themePropertiesTabbedPanel, BorderLayout.CENTER);
429: contentPane.add(controlPanel, BorderLayout.SOUTH);
430:
431: }
432:
433: private Shader selectShader(String type) {
434: if (type.equals("Mono"))
435: return new MonoShader();
436: else if (type.equals("Random"))
437: return new RandomShader();
438: else if (type.equals("Continuous"))
439: return new HSVShader();
440: else if (type.equals("Unique"))
441: return new UniqueShader();
442: else
443: System.out.println("### PROBLEM selecting shader ...");
444: return null;
445: }
446:
447: private String getShaderName(Shader shader) {
448: if (shader instanceof MonoShader)
449: return "Mono";
450: else if (shader instanceof RandomShader)
451: return "Random";
452: else if (shader instanceof HSVShader)
453: return "Continuous";
454: else if (shader instanceof UniqueShader)
455: return "Unique";
456: else
457: return "### PROBLEM selecting shader name !!";
458: }
459:
460: private String[] getFieldNames() {
461: String[] fieldsName = new String[0];
462: if (getObjectViewerThemeProperties().getDataSourceType()
463: .equals(Utils.SHAPEFILE)) {
464: ShapefileReader shpRd = (ShapefileReader) getObjectViewerThemeProperties()
465: .getDataSource();
466: Dbf dbf = shpRd.dbf;
467: fieldsName = new String[dbf.getNumFields()];
468: for (int i = 0; i < fieldsName.length; i++)
469: fieldsName[i] = dbf.getFieldName(i).toString();
470: }
471:
472: else if (getObjectViewerThemeProperties().getDataSourceType()
473: .equals(Utils.GEOXYGENE)) {
474: GeOxygeneReader geOxyRd = (GeOxygeneReader) getObjectViewerThemeProperties()
475: .getDataSource();
476: fieldsName = geOxyRd.getFieldsNames();
477: }
478: return fieldsName;
479: }
480:
481: private String[] getDoubleOrIntFields() {
482: Vector vector = new Vector();
483: String[] allFields = getFieldNames();
484: for (int i = 0; i < allFields.length; i++)
485: if (getSelectedFieldClass(allFields[i])
486: .equals(double.class)
487: || getSelectedFieldClass(allFields[i]).equals(
488: int.class))
489: vector.add(allFields[i]);
490: String[] array = new String[vector.size()];
491: for (int i = 0; i < vector.size(); i++)
492: array[i] = (String) vector.get(i);
493: return array;
494: }
495:
496: private String[] getStringFields() {
497: Vector vector = new Vector();
498: String[] allFields = getFieldNames();
499: for (int i = 0; i < allFields.length; i++)
500: if (getSelectedFieldClass(allFields[i])
501: .equals(String.class))
502: vector.add(allFields[i]);
503: String[] array = new String[vector.size()];
504: for (int i = 0; i < vector.size(); i++)
505: array[i] = (String) vector.get(i);
506: return array;
507: }
508:
509: private Class getSelectedFieldClass(String fieldName) {
510: Class result = null;
511: if (getObjectViewerThemeProperties().getDataSourceType()
512: .equals(Utils.GEOXYGENE)) {
513: GeOxygeneReader geOxyRd = (GeOxygeneReader) getObjectViewerThemeProperties()
514: .getDataSource();
515: result = geOxyRd.getFieldType(fieldName);
516: }
517: return result;
518: }
519:
520: private ObjectViewerThemeProperties getObjectViewerThemeProperties() {
521: return objectViewerThemeProperties;
522: }
523:
524: }
|