001: /*
002: * Created on 27.09.2005
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package de.latlon.deejump.plugin.style;
008:
009: import java.awt.BorderLayout;
010: import java.awt.Dimension;
011: import java.awt.Image;
012: import java.awt.MediaTracker;
013: import java.awt.Toolkit;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.io.File;
017: import java.util.ArrayList;
018: import java.util.Collections;
019: import java.util.Hashtable;
020: import java.util.List;
021:
022: import javax.swing.BorderFactory;
023: import javax.swing.JButton;
024: import javax.swing.JComboBox;
025: import javax.swing.JFileChooser;
026: import javax.swing.JLabel;
027: import javax.swing.JPanel;
028: import javax.swing.JSlider;
029: import javax.swing.event.ChangeListener;
030: import javax.swing.filechooser.FileFilter;
031:
032: import com.vividsolutions.jump.I18N;
033: import com.vividsolutions.jump.workbench.ui.renderer.style.VertexStyle;
034:
035: /**
036: * @author hamammi
037: *
038: * TODO To change the template for this generated type comment go to
039: * Window - Preferences - Java - Code Style - Code Templates
040: *
041: * [sstein 02.08.2006] - removed point size slider
042: */
043: public class VertexStyleChooser extends JPanel {
044:
045: private static final List STYLE_NAMES;
046:
047: static {
048:
049: List TEMP_STYLE_NAMES = new ArrayList(5);
050: TEMP_STYLE_NAMES.add(VertexStylesFactory.SQUARE_STYLE);
051: TEMP_STYLE_NAMES.add(VertexStylesFactory.CIRCLE_STYLE);
052: TEMP_STYLE_NAMES.add(VertexStylesFactory.TRIANGLE_STYLE);
053: TEMP_STYLE_NAMES.add(VertexStylesFactory.CROSS_STYLE);
054: TEMP_STYLE_NAMES.add(VertexStylesFactory.STAR_STYLE);
055: TEMP_STYLE_NAMES.add(VertexStylesFactory.BITMAP_STYLE);
056:
057: STYLE_NAMES = Collections.unmodifiableList(TEMP_STYLE_NAMES);
058: }
059:
060: private JComboBox pointTypeComboBox;
061: private JButton bitmapChangeButton;
062: private String selectedItem;
063: private String currentFilename;
064: //[sstein 02.08.2006] - removed because we would have two sliders
065: public JSlider sizeSlider;
066: private boolean activateOwnSlider = false;
067:
068: public VertexStyleChooser(boolean activateOwnSlider) {
069: super ();
070: initGUI();
071: this .activateOwnSlider = activateOwnSlider;
072: }
073:
074: private void initGUI() {
075: pointTypeComboBox = new JComboBox();
076: pointTypeComboBox.setEditable(false);
077: pointTypeComboBox.addItem(I18N
078: .get("deejump.ui.style.RenderingStylePanel.square"));
079: pointTypeComboBox.addItem(I18N
080: .get("deejump.ui.style.RenderingStylePanel.circle"));
081: pointTypeComboBox.addItem(I18N
082: .get("deejump.ui.style.RenderingStylePanel.triangle"));
083: pointTypeComboBox.addItem(I18N
084: .get("deejump.ui.style.RenderingStylePanel.cross"));
085: pointTypeComboBox.addItem(I18N
086: .get("deejump.ui.style.RenderingStylePanel.star"));
087: pointTypeComboBox.addItem(I18N
088: .get("deejump.ui.style.RenderingStylePanel.bitmap"));
089:
090: pointTypeComboBox.addActionListener(new ActionListener() {
091: public void actionPerformed(ActionEvent e) {
092: JComboBox comboBox = (JComboBox) e.getSource();
093: String selectedItem = (String) STYLE_NAMES.get(comboBox
094: .getSelectedIndex());
095: if (selectedItem
096: .equals(VertexStylesFactory.BITMAP_STYLE)) {
097: OpenFileChooser();
098: }
099: setSelectedStyle(selectedItem);
100: setSelectedItem(selectedItem);
101: }
102: });
103:
104: bitmapChangeButton = new JButton(
105: I18N
106: .get("deejump.ui.style.RenderingStylePanel.bitmap-change"));
107: bitmapChangeButton.addActionListener(new ActionListener() {
108: public void actionPerformed(ActionEvent arg0) {
109: OpenFileChooser();
110: }
111: });
112: if (sizeSlider == null) {
113: sizeSlider = new JSlider(); //[sstein] init only if needed
114: }
115: sizeSlider.setBorder(BorderFactory
116: .createTitledBorder("Point size: "));
117: if (this .activateOwnSlider == true) {
118: Hashtable labelTable = new Hashtable();
119: labelTable.put(new Integer(5), new JLabel("5"));
120: labelTable.put(new Integer(10), new JLabel("10"));
121: labelTable.put(new Integer(15), new JLabel("15"));
122: labelTable.put(new Integer(20), new JLabel("20"));
123: sizeSlider.setLabelTable(labelTable);
124: sizeSlider.setEnabled(true);
125: sizeSlider.setMajorTickSpacing(1);
126: sizeSlider.setMajorTickSpacing(0);
127: sizeSlider.setPaintLabels(true);
128: sizeSlider.setMinimum(4);
129: sizeSlider.setValue(4);
130: sizeSlider.setMaximum(20);
131: sizeSlider.setSnapToTicks(false);
132: sizeSlider.setPreferredSize(new Dimension(130, 49));
133: }
134: JPanel oberstPanel = new JPanel();
135: oberstPanel
136: .add(new JLabel(
137: I18N
138: .get("deejump.ui.style.RenderingStylePanel.point-display-type")));
139: oberstPanel.add(pointTypeComboBox);
140: oberstPanel.add(bitmapChangeButton);
141: JPanel sliderPanel = new JPanel(); //[sstein] always init although it may not be needed
142: sliderPanel.add(sizeSlider);
143: setLayout(new BorderLayout());
144: add(oberstPanel, BorderLayout.NORTH);
145: if (this .activateOwnSlider == true) {
146: add(sliderPanel, BorderLayout.CENTER);
147: }
148:
149: }
150:
151: public void addActionListener(ActionListener actionListener) {
152: pointTypeComboBox.addActionListener(actionListener);
153: bitmapChangeButton.addActionListener(actionListener);
154:
155: }
156:
157: public void removeActionListener(ActionListener actionListener) {
158: pointTypeComboBox.removeActionListener(actionListener);
159: bitmapChangeButton.removeActionListener(actionListener);
160: }
161:
162: private void setSelectedItem(String selectedItem) {
163: this .selectedItem = selectedItem;
164:
165: }
166:
167: public void addChangeListener(ChangeListener cl) {
168: if (this .activateOwnSlider == true) {
169: this .sizeSlider.addChangeListener(cl);
170: }
171: }
172:
173: public void removeChangeListener(ChangeListener cl) {
174: if (this .activateOwnSlider == true) {
175: this .sizeSlider.removeChangeListener(cl);
176: }
177: }
178:
179: //GH 2005.09.15 this Methode opens a JFileChooser and returns true if the Image
180: // be loaded sucssfully.
181: public boolean OpenFileChooser() {
182: boolean imageIsLoaded = false;
183: JFileChooser fileChooser = new JFileChooser();
184: fileChooser.setFileFilter(new FileFilter() {
185: public boolean accept(File file) {
186: return file.isDirectory()
187: || file.getName().toLowerCase()
188: .endsWith(".png")
189: || file.getName().toLowerCase()
190: .endsWith(".gif")
191: || file.getName().toLowerCase()
192: .endsWith(".jpg");
193: }
194:
195: public String getDescription() {
196: return "Bitmap";
197: }
198: });
199: int showFileChooser = fileChooser.showOpenDialog(this );
200: if (showFileChooser == JFileChooser.APPROVE_OPTION) {
201: String currentFilePath;
202: currentFilePath = fileChooser.getSelectedFile()
203: .getAbsolutePath();
204: Image image = Toolkit.getDefaultToolkit().getImage(
205: currentFilePath);
206: setCurrentFileName(currentFilePath);
207: MediaTracker mediaTracker = new MediaTracker(this );
208: mediaTracker.addImage(image, 0);
209: try {
210: mediaTracker.waitForAll();
211: imageIsLoaded = true;
212: } catch (InterruptedException e) {
213: //nothing
214: }
215: } else {
216: // user cancelled dialog
217: }
218: return imageIsLoaded;
219: }
220:
221: private void setCurrentFileName(String fileName) {
222: currentFilename = fileName;
223: }
224:
225: public String getCurrentFileName() {
226: return currentFilename;
227: }
228:
229: public void setEnabled(boolean enabled) {
230: super .setEnabled(enabled);
231: this .pointTypeComboBox.setEnabled(enabled);
232: this .bitmapChangeButton.setEnabled(enabled);
233: if (this .activateOwnSlider == true) {
234: this .sizeSlider.setEnabled(enabled);
235: }
236: }
237:
238: /**
239: * @return the selected vertex style
240: */
241: public VertexStyle getSelectedStyle() {
242: String wellKnowName = (String) STYLE_NAMES
243: .get(this .pointTypeComboBox.getSelectedIndex());
244: //-- sstein [08.08.2006] changed a bit to avoid null pointer exception if no file has been
245: // specified
246: if (VertexStylesFactory.BITMAP_STYLE.equals(wellKnowName)) {
247: wellKnowName = this .getCurrentFileName();
248: //String s = getCurrentFileName();
249: //System.out.println(s);
250: if (getCurrentFileName() != null) {
251: wellKnowName = this .getCurrentFileName();
252: } else {
253: //reset to the first style
254: wellKnowName = (String) STYLE_NAMES.get(0);
255: }
256: }
257: VertexStyle vertexStyle = VertexStylesFactory
258: .createVertexStyle(wellKnowName);
259: if (!(vertexStyle instanceof BitmapVertexStyle)) {
260: vertexStyle.setSize(sizeSlider.getValue());
261: }
262: return vertexStyle;
263: }
264:
265: /**
266: * @param currentVertexStyle
267: */
268: public void setSelectedStyle(String currentVertexStyle) {
269: int nameIndex = STYLE_NAMES.indexOf(currentVertexStyle);
270: if (nameIndex > -1 && nameIndex < STYLE_NAMES.size()) {
271: this.pointTypeComboBox.setSelectedIndex(nameIndex);
272: }
273: }
274: }
|