gvsig-geoprocess / org.gvsig.sextante / trunk / org.gvsig.sextante.app / org.gvsig.sextante.app.extension / src / main / java / org / gvsig / sextante / app / extension / ToolbarConfigDialog.java @ 43
History | View | Annotate | Download (6.77 KB)
1 |
package org.gvsig.sextante.app.extension; |
---|---|
2 |
|
3 |
import info.clearthought.layout.TableLayout; |
4 |
|
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.io.BufferedReader; |
8 |
import java.io.BufferedWriter; |
9 |
import java.io.File; |
10 |
import java.io.FileInputStream; |
11 |
import java.io.FileOutputStream; |
12 |
import java.io.FileReader; |
13 |
import java.io.FileWriter; |
14 |
import java.io.IOException; |
15 |
import java.io.InputStream; |
16 |
import java.io.OutputStream; |
17 |
|
18 |
import javax.swing.JButton; |
19 |
import javax.swing.JDialog; |
20 |
import javax.swing.JOptionPane; |
21 |
import javax.swing.JScrollPane; |
22 |
import javax.swing.JTable; |
23 |
import javax.swing.table.DefaultTableCellRenderer; |
24 |
import javax.swing.table.TableColumnModel; |
25 |
import javax.swing.table.TableModel; |
26 |
|
27 |
import org.gvsig.andami.PluginServices; |
28 |
|
29 |
import es.unex.sextante.core.Sextante; |
30 |
import es.unex.sextante.gui.core.SextanteGUI; |
31 |
|
32 |
public class ToolbarConfigDialog extends JDialog { |
33 |
private static final long serialVersionUID = 1L; |
34 |
private static final String BASE_CONFIG_FILE = "config_base.xml"; |
35 |
private static final String CONFIG_FILE = "config.xml"; |
36 |
|
37 |
private JTable jTable; |
38 |
private JButton jButtonCancel; |
39 |
private JButton jButtonRemove; |
40 |
private JButton jButtonAdd; |
41 |
private JScrollPane jScrollPane; |
42 |
private JButton jButtonOK; |
43 |
|
44 |
|
45 |
public ToolbarConfigDialog(){
|
46 |
|
47 |
super(SextanteGUI.getMainFrame(), "SextanteToolbar", true); |
48 |
|
49 |
this.setResizable(true); |
50 |
|
51 |
initGUI(); |
52 |
|
53 |
} |
54 |
|
55 |
private void initGUI() { |
56 |
|
57 |
try {
|
58 |
TableLayout thisLayout = new TableLayout(new double[][] { |
59 |
{7.0, 50.0, 50.0, TableLayout.FILL, TableLayout.FILL, TableLayout.FILL, 7.0}, |
60 |
{10.0, TableLayout.FILL, TableLayout.FILL, 30.0, 30.0, 30.0, 7.0}}); |
61 |
thisLayout.setHGap(5);
|
62 |
thisLayout.setVGap(5);
|
63 |
getContentPane().setLayout(thisLayout); |
64 |
{ |
65 |
jButtonOK = new JButton(); |
66 |
getContentPane().add(jButtonOK, "4, 5");
|
67 |
jButtonOK.setText(PluginServices.getText(this, "OK")); |
68 |
jButtonOK.addActionListener(new ActionListener() { |
69 |
public void actionPerformed(ActionEvent evt) { |
70 |
jButtonOKActionPerformed(evt); |
71 |
} |
72 |
}); |
73 |
} |
74 |
{ |
75 |
jButtonCancel = new JButton(); |
76 |
getContentPane().add(jButtonCancel, "5, 5");
|
77 |
jButtonCancel.setText(PluginServices.getText(this, "Cancel")); |
78 |
jButtonCancel.addActionListener(new ActionListener() { |
79 |
public void actionPerformed(ActionEvent evt) { |
80 |
jButtonCancelActionPerformed(evt); |
81 |
} |
82 |
}); |
83 |
} |
84 |
{ |
85 |
jScrollPane = new JScrollPane(); |
86 |
getContentPane().add(jScrollPane, "1, 0, 5, 2");
|
87 |
updateTable(); |
88 |
} |
89 |
{ |
90 |
jButtonAdd = new JButton(); |
91 |
getContentPane().add(jButtonAdd, "1, 3");
|
92 |
jButtonAdd.setText("+");
|
93 |
jButtonAdd.addActionListener(new ActionListener() { |
94 |
public void actionPerformed(ActionEvent evt) { |
95 |
jButtonAddActionPerformed(evt); |
96 |
} |
97 |
}); |
98 |
} |
99 |
{ |
100 |
jButtonRemove = new JButton(); |
101 |
getContentPane().add(jButtonRemove, "2, 3");
|
102 |
jButtonRemove.setText("-");
|
103 |
jButtonRemove.addActionListener(new ActionListener() { |
104 |
public void actionPerformed(ActionEvent evt) { |
105 |
jButtonRemoveActionPerformed(evt); |
106 |
} |
107 |
}); |
108 |
} |
109 |
pack(); |
110 |
this.setSize(519, 255); |
111 |
} catch(Exception e) { |
112 |
e.printStackTrace(); |
113 |
} |
114 |
} |
115 |
|
116 |
private void updateTable() { |
117 |
|
118 |
TableModel tableModel = new ButtonTableModel(SextanteToolbarManager.getButtons()); |
119 |
jTable = new JTable(); |
120 |
jScrollPane.setViewportView(jTable); |
121 |
jTable.setModel(tableModel); |
122 |
|
123 |
TableColumnModel tcm = jTable.getColumnModel();
|
124 |
tcm.getColumn(0).setPreferredWidth(30); |
125 |
DefaultTableCellRenderer renderer = new IconCellRenderer(); |
126 |
tcm.getColumn(0).setCellRenderer(renderer);
|
127 |
|
128 |
} |
129 |
|
130 |
private void jButtonAddActionPerformed(ActionEvent evt) { |
131 |
|
132 |
ButtonDefinitionDialog dialog = new ButtonDefinitionDialog(this); |
133 |
dialog.pack(); |
134 |
dialog.setVisible(true);
|
135 |
Button button = dialog.getButton();
|
136 |
if (button != null){ |
137 |
SextanteToolbarManager.addButton(button); |
138 |
updateTable(); |
139 |
} |
140 |
|
141 |
} |
142 |
|
143 |
private void jButtonRemoveActionPerformed(ActionEvent evt) { |
144 |
|
145 |
int iRow;
|
146 |
|
147 |
iRow = jTable.getSelectedRow(); |
148 |
if (iRow != -1){ |
149 |
((ButtonTableModel)jTable.getModel()).removeRow(iRow); |
150 |
} |
151 |
|
152 |
} |
153 |
|
154 |
private void jButtonOKActionPerformed(ActionEvent evt) { |
155 |
|
156 |
saveChanges(); |
157 |
dispose(); |
158 |
setVisible(false);
|
159 |
JOptionPane.showMessageDialog(null, |
160 |
PluginServices.getText(this, "You_need_to_restart"), |
161 |
PluginServices.getText(this, "Warning"), |
162 |
JOptionPane.INFORMATION_MESSAGE);
|
163 |
|
164 |
} |
165 |
|
166 |
private void saveChanges() { |
167 |
|
168 |
SextanteToolbarManager.save(); |
169 |
|
170 |
String sPath = System.getProperty("user.dir") + File.separator + "gvSIG" |
171 |
+ File.separator + "extensiones" |
172 |
+ File.separator + "es.unex.sextante"; |
173 |
|
174 |
File inFile = new File(sPath + File.separator + BASE_CONFIG_FILE); |
175 |
File outFile = new File(sPath + File.separator + CONFIG_FILE); |
176 |
try {
|
177 |
BufferedReader reader = new BufferedReader(new FileReader(inFile)); |
178 |
BufferedWriter writer = new BufferedWriter(new FileWriter(outFile)); |
179 |
|
180 |
String line = null; |
181 |
while ((line=reader.readLine()) != null) { |
182 |
writer.write(line); |
183 |
writer.newLine(); |
184 |
} |
185 |
|
186 |
reader.close(); |
187 |
|
188 |
|
189 |
for (int i = 0; i < SextanteToolbarManager.getButtons().size(); i++) { |
190 |
Button button = SextanteToolbarManager.getButton(i);
|
191 |
String sIconFilename = button.sIconFilename;
|
192 |
if (sIconFilename == null || sIconFilename.trim().equals("")){ |
193 |
sIconFilename = "module2.png";
|
194 |
} |
195 |
else{
|
196 |
File iconFile = new File (sIconFilename); |
197 |
sIconFilename = (iconFile.getName()); |
198 |
File outputFile = new File(sPath + File.separator + "images" + File.separator + iconFile.getName()); |
199 |
InputStream in = new FileInputStream(iconFile); |
200 |
OutputStream out = new FileOutputStream(outputFile); |
201 |
byte[] buf = new byte[1024]; |
202 |
int len;
|
203 |
while ((len = in.read(buf)) > 0){ |
204 |
out.write(buf, 0, len);
|
205 |
} |
206 |
in.close(); |
207 |
out.close(); |
208 |
} |
209 |
writer.write("<extension class-name=\"es.unex.sextante.gvsig.extensions.SextanteAlgorithmExecutorExtension\"\n");
|
210 |
writer.write("\t description=\"" + button.sName + "\""); |
211 |
writer.write("\t active=\"true\">\n");
|
212 |
writer.write("\t <tool-bar name=\"SEXTANTEToolbar\" position=\"3\">\n"
|
213 |
+"<action-tool icon=\"images/" + sIconFilename.trim() + "\" tooltip=\"" + button.sName |
214 |
+ "\" position=\"" + Integer.toString(i+2) |
215 |
+ "\" action-command=\"" + Integer.toString(i) + "\"/>\n" + |
216 |
"</tool-bar>");
|
217 |
writer.write("</extension>\n");
|
218 |
} |
219 |
writer.write("</extensions>\n");
|
220 |
writer.write("</plugin-config>");
|
221 |
|
222 |
writer.close(); |
223 |
|
224 |
} catch (IOException e) { |
225 |
Sextante.addErrorToLog(e); |
226 |
|
227 |
} |
228 |
|
229 |
} |
230 |
|
231 |
private void jButtonCancelActionPerformed(ActionEvent evt) { |
232 |
|
233 |
dispose(); |
234 |
setVisible(false);
|
235 |
|
236 |
} |
237 |
|
238 |
} |