001: //$Header$
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2005 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/exse/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstraße 19
030: 53177 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043:
044: package de.latlon.deejump.plugin;
045:
046: import java.awt.Color;
047: import java.awt.Component;
048: import java.awt.Dimension;
049: import java.awt.Graphics2D;
050: import java.awt.GridBagConstraints;
051: import java.awt.GridBagLayout;
052: import java.awt.Insets;
053: import java.awt.image.BufferedImage;
054: import java.awt.image.RenderedImage;
055: import java.io.File;
056: import java.io.IOException;
057:
058: import javax.imageio.ImageIO;
059: import javax.swing.JFileChooser;
060: import javax.swing.JFrame;
061: import javax.swing.JLabel;
062: import javax.swing.JPanel;
063: import javax.swing.JTree;
064: import javax.swing.tree.DefaultMutableTreeNode;
065: import javax.swing.tree.TreeCellRenderer;
066:
067: import com.vividsolutions.jump.I18N;
068: import com.vividsolutions.jump.workbench.model.Layer;
069: import com.vividsolutions.jump.workbench.model.LayerTreeModel;
070: import com.vividsolutions.jump.workbench.plugin.AbstractPlugIn;
071: import com.vividsolutions.jump.workbench.plugin.PlugInContext;
072: import com.vividsolutions.jump.workbench.ui.ColorPanel;
073: import com.vividsolutions.jump.workbench.ui.GUIUtil;
074: import com.vividsolutions.jump.workbench.ui.LayerNamePanel;
075: import com.vividsolutions.jump.workbench.ui.TreeLayerNamePanel;
076: import com.vividsolutions.jump.workbench.ui.renderer.style.BasicStyle;
077:
078: /**
079: * ...
080: *
081: * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
082: * @author last edited by: $Author: mentaer $
083: *
084: * @version 2.0, $Revision: 333 $, $Date: 2006-08-06 09:48:28 -0700 (Sun, 06 Aug 2006) $
085: *
086: * @since 2.0
087: */
088:
089: public class SaveLegendPlugIn extends AbstractPlugIn {
090:
091: public SaveLegendPlugIn() {
092: //nothing
093: }
094:
095: private String filename = null;
096:
097: public void initialize(PlugInContext context) throws Exception {
098:
099: context.getFeatureInstaller().addPopupMenuItem(
100: context.getWorkbenchContext().getWorkbench().getFrame()
101: .getLayerNamePopupMenu(), this ,
102: this .getName() + "{pos:13}", false, null, null);
103: }
104:
105: public boolean execute(PlugInContext context) throws Exception {
106:
107: final Layer layer = context.getSelectedLayer(0);
108:
109: LayerNamePanel layerPanel = context.getLayerNamePanel();
110: if (!(layerPanel instanceof TreeLayerNamePanel)) {
111: return false;
112:
113: }
114:
115: JTree newTree = createLayerTree(layer,
116: (TreeLayerNamePanel) layerPanel);
117:
118: saveLegend(context, newTree);
119:
120: return true;
121: }
122:
123: private void saveLegend(PlugInContext context, JTree tree)
124: throws IOException {
125:
126: JFileChooser fileChooser = new JFileChooser();
127: fileChooser.setApproveButtonText(I18N
128: .get("deejump.plugin.SaveLegendPlugIn.Save"));
129: fileChooser
130: .setDialogTitle(I18N
131: .get("deejump.plugin.SaveLegendPlugIn.Save-legend-as-image-png"));
132:
133: JPanel p = new JPanel();
134: p.add(tree);
135:
136: fileChooser.setAccessory(p);
137:
138: if (JFileChooser.APPROVE_OPTION == fileChooser
139: .showOpenDialog(context.getWorkbenchFrame())) {
140:
141: // can only save if has a sizw -> put in a frame
142: JFrame f = new JFrame(
143: I18N
144: .get("deejump.plugin.SaveLegendPlugIn.Save-legend-as-image-png"));
145: f.getContentPane().add(tree);
146: f.pack();
147: // f.setVisible( true );
148:
149: saveComponentAsJPEG(tree, fileChooser.getSelectedFile()
150: .getAbsolutePath());
151:
152: f.setVisible(false);
153: f.dispose();
154:
155: }
156:
157: }
158:
159: private JTree createLayerTree(Layer layer,
160: TreeLayerNamePanel treePanel) {
161:
162: JTree tree = treePanel.getTree();
163:
164: DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(
165: layer.getName());
166:
167: for (int j = 0; j < tree.getModel().getChildCount(layer); j++) {
168: rootNode.add(new DefaultMutableTreeNode(tree.getModel()
169: .getChild(layer, j)));
170: }
171: JTree newTree = new JTree(rootNode);
172: newTree.setCellRenderer(createColorThemingValueRenderer());
173:
174: return newTree;
175: }
176:
177: public String getName() {
178: return I18N.get("deejump.plugin.SaveLegendPlugIn.Save-legend");
179: }
180:
181: public static void saveComponentAsJPEG(Component myComponent,
182: String filename)
183:
184: throws IOException {
185: //-- [sstein, 06.08.2006]
186: if (!filename.endsWith(".png")) {
187: filename = filename + ".png";
188: }
189: //--
190: Dimension size = myComponent.getSize();
191: BufferedImage myImage = new BufferedImage(size.width,
192: size.height, BufferedImage.TYPE_INT_RGB);
193: Graphics2D g2 = myImage.createGraphics();
194: myComponent.paint(g2);
195:
196: ImageIO.write((RenderedImage) myImage, "PNG",
197: new File(filename));
198:
199: }
200:
201: private TreeCellRenderer createColorThemingValueRenderer() {
202: return new TreeCellRenderer() {
203: private JPanel panel = new JPanel(new GridBagLayout());
204: private ColorPanel colorPanel = new ColorPanel();
205: private JLabel label = new JLabel();
206: {
207: panel.add(colorPanel, new GridBagConstraints(0, 0, 1,
208: 1, 0, 0, GridBagConstraints.WEST,
209: GridBagConstraints.NONE,
210: new Insets(0, 0, 0, 0), 0, 0));
211:
212: panel.add(label, new GridBagConstraints(1, 0, 1, 1, 0,
213: 0, GridBagConstraints.WEST,
214: GridBagConstraints.NONE,
215: new Insets(0, 5, 0, 0), 0, 0));
216:
217: panel.setBackground(Color.white);
218: label.setBackground(Color.white);
219:
220: }
221:
222: public Component getTreeCellRendererComponent(JTree tree,
223: Object value, boolean selected, boolean expanded,
224: boolean leaf, int row, boolean hasFocus) {
225:
226: String txt = "";
227:
228: DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
229: Object userObject = node.getUserObject();
230:
231: if (userObject instanceof LayerTreeModel.ColorThemingValue) {
232:
233: LayerTreeModel.ColorThemingValue entry = (LayerTreeModel.ColorThemingValue) userObject;
234:
235: txt = entry.toString();
236:
237: BasicStyle style = entry.getStyle();
238: colorPanel
239: .setLineColor(style.isRenderingLine() ? GUIUtil
240: .alphaColor(style.getLineColor(),
241: style.getAlpha())
242: : GUIUtil
243: .alphaColor(Color.BLACK, 0));
244: colorPanel
245: .setFillColor(style.isRenderingFill() ? GUIUtil
246: .alphaColor(style.getFillColor(),
247: style.getAlpha())
248: : GUIUtil
249: .alphaColor(Color.BLACK, 0));
250:
251: } else {
252:
253: txt = (String) userObject;
254: colorPanel.setFillColor(Color.white);
255: colorPanel.setLineColor(Color.white);
256: }
257:
258: label.setText(txt);
259:
260: return panel;
261: }
262: };
263: }
264:
265: }
266:
267: /* ********************************************************************
268: Changes to this class. What the people have been up to:
269: $Log$
270: Revision 1.2 2006/08/06 16:48:28 mentaer
271: changed menu pos
272: improved SaveLegendPlugIn by adding file ending ".png"
273:
274: Revision 1.1 2006/08/06 16:22:11 mentaer
275: added savelegend
276:
277: Revision 1.1 2006/03/06 09:42:11 ut
278: latest changes (key pan, legend, etc)
279:
280:
281: ********************************************************************** */
|