source: osm/applications/editors/josm/plugins/smed2/src/panels/PanelMain.java@ 30314

Last change on this file since 30314 was 30314, checked in by malcolmh, 11 years ago

save

File size: 6.4 KB
Line 
1/* Copyright 2014 Malcolm Herring
2 *
3 * This is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License.
6 *
7 * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
8 */
9
10package panels;
11
12import static org.openstreetmap.josm.tools.I18n.tr;
13
14import java.awt.Color;
15import java.awt.Dimension;
16import java.awt.Graphics;
17import java.awt.Graphics2D;
18import java.awt.event.ActionListener;
19import java.awt.image.BufferedImage;
20import java.io.*;
21
22import javax.imageio.ImageIO;
23import javax.swing.*;
24
25import messages.Messages;
26
27import org.openstreetmap.josm.Main;
28
29import s57.S57att.*;
30import s57.S57obj.*;
31import s57.S57map.*;
32import render.Renderer;
33import smed2.Smed2Action;
34
35public class PanelMain extends JPanel {
36
37 BufferedImage img;
38 int w, h, z, f;
39 JTextField wt, ht, zt, ft;
40 public static JTextArea decode = null;
41 public static JTextField messageBar = null;
42 public JButton saveButton = null;
43 private ActionListener alSave = new ActionListener() {
44 public void actionPerformed(java.awt.event.ActionEvent e) {
45 dumpMap();
46 }
47 };
48 private JButton importButton = null;
49 JFileChooser ifc = new JFileChooser(Main.pref.get("smed2plugin.encinpfile"));
50 private ActionListener alImport = new ActionListener() {
51 public void actionPerformed(java.awt.event.ActionEvent e) {
52 if (e.getSource() == importButton) {
53 Smed2Action.panelS57.setVisible(true);
54 setStatus("Select S-57 ENC file for import", Color.yellow);
55 int returnVal = ifc.showOpenDialog(Main.parent);
56 if (returnVal == JFileChooser.APPROVE_OPTION) {
57 try {
58 Main.pref.put("smed2plugin.encinpfile", ifc.getSelectedFile().getPath());
59 Smed2Action.panelS57.startImport(ifc.getSelectedFile());
60 } catch (IOException e1) {
61 Smed2Action.panelS57.setVisible(false);
62 setStatus("IO Exception", Color.red);
63 }
64 } else {
65 Smed2Action.panelS57.setVisible(false);
66 clrStatus();
67 }
68 }
69 }
70 };
71
72 private JButton exportButton = null;
73 final JFileChooser efc = new JFileChooser();
74 private ActionListener alExport = new ActionListener() {
75 public void actionPerformed(java.awt.event.ActionEvent e) {
76 if (e.getSource() == exportButton) {
77 Smed2Action.panelS57.setVisible(true);
78 setStatus("Select S-57 ENC file for export", Color.yellow);
79 int returnVal = efc.showOpenDialog(Main.parent);
80 if (returnVal == JFileChooser.APPROVE_OPTION) {
81 try {
82 Smed2Action.panelS57.startExport(efc.getSelectedFile());
83 } catch (IOException e1) {
84 Smed2Action.panelS57.setVisible(false);
85 setStatus("IO Exception", Color.red);
86 }
87 } else {
88 Smed2Action.panelS57.setVisible(false);
89 clrStatus();
90 }
91 }
92 }
93 };
94
95 public PanelMain() {
96 setLayout(null);
97 setSize(new Dimension(480, 480));
98
99 w = h = z = f = 0;
100 wt = new JTextField("0");
101 wt.setBounds(10, 400, 40, 20);
102 add(wt);
103 ht = new JTextField("0");
104 ht.setBounds(60, 400, 40, 20);
105 add(ht);
106 zt = new JTextField("0");
107 zt.setBounds(110, 400, 40, 20);
108 add(zt);
109 ft = new JTextField("0");
110 ft.setBounds(160, 400, 40, 20);
111 add(ft);
112
113 messageBar = new JTextField();
114 messageBar.setBounds(70, 430, 290, 20);
115 messageBar.setEditable(false);
116 messageBar.setBackground(Color.WHITE);
117 add(messageBar);
118 importButton = new JButton(new ImageIcon(getClass().getResource("/images/importButton.png")));
119 importButton.setBounds(10, 430, 20, 20);
120 add(importButton);
121 importButton.addActionListener(alImport);
122 exportButton = new JButton(new ImageIcon(getClass().getResource("/images/exportButton.png")));
123 exportButton.setBounds(40, 430, 20, 20);
124 add(exportButton);
125 exportButton.addActionListener(alExport);
126 saveButton = new JButton();
127 saveButton.setBounds(370, 430, 100, 20);
128 saveButton.setText(tr("Save"));
129 add(saveButton);
130 saveButton.addActionListener(alSave);
131
132 decode = new JTextArea();
133 decode.setBounds(0, 0, 480, 420);
134 decode.setTabSize(1);
135 add(decode);
136 }
137
138 public void dumpMap() {
139 img = new BufferedImage(Integer.parseInt(wt.getText()), Integer.parseInt(ht.getText()), BufferedImage.TYPE_INT_ARGB);
140 Graphics2D g2 = img.createGraphics();
141 Renderer.reRender(g2, Integer.parseInt(zt.getText()), Integer.parseInt(ft.getText()), Smed2Action.map, Smed2Action.rendering);
142 try {
143 ImageIO.write(img, "png", new File("/Users/mherring/Desktop/export.png"));
144 } catch (Exception x) {
145 System.out.println("Exception");
146 }
147 repaint();
148 }
149
150 public void paintComponent(Graphics g){
151 super.paintComponent(g);
152 Graphics2D g2 = (Graphics2D) g;
153 g2.setBackground(new Color(0xb5d0d0));
154 if (img != null) g2.clearRect(0, 0, img.getWidth(), img.getHeight());
155 g2.drawImage(img, 0, 0, null);
156 }
157
158 public static void setStatus(String text, Color bg) {
159 messageBar.setBackground(bg);
160 messageBar.setText(text);
161 }
162
163 public static void clrStatus() {
164 messageBar.setBackground(Color.white);
165 messageBar.setText("");
166 }
167
168 public void parseMark(Feature feature) {
169 decode.setText("Selected object:\n");
170 decode.append("\t" + tr("Type") + ": " + Messages.getString(feature.type.name()) + "\n");
171 if (feature.atts.get(Att.OBJNAM) != null) {
172 decode.append("\t" + tr("Name") + ": " + feature.atts.get(Att.OBJNAM).val + "\n");
173 }
174 decode.append("\tObjects:\n");
175 for (Obj obj : feature.objs.keySet()) {
176 decode.append("\t\t" + Messages.getString(obj.name()) + "\n");
177/* if (feature.aggr.objs.get(obj).size() != 0) {
178 for (AttMap atts : feature.objs.get(obj).values()) {
179 for (Att att : atts.keySet()) {
180 AttVal<?> item = atts.get(att);
181 switch (item.conv) {
182 case E:
183 decode.append("\t\t\t" + Messages.getString(att.name()) + ": " + Messages.getString(((Enum<?>)item.val).name()) + "\n");
184 break;
185 case L:
186 decode.append("\t\t\t" + Messages.getString(att.name()) + ": ");
187 Iterator<?> it = ((ArrayList<?>)item.val).iterator();
188 while (it.hasNext()) {
189 Object val = it.next();
190 decode.append(Messages.getString(((Enum<?>)val).name()));
191 if (it.hasNext()) {
192 decode.append(", ");
193 }
194 }
195 decode.append("\n");
196 break;
197 default:
198 decode.append("\t\t\t" + Messages.getString(att.name()) + ": " + item.val + "\n");
199 }
200 }
201 }
202 }
203*/ }
204 }
205
206 public void clearMark() {
207 decode.setText(tr("No object selected"));
208 }
209
210}
Note: See TracBrowser for help on using the repository browser.