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

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