root / tags / v1_0_2_Build_912 / applications / appgvSIG / src / com / iver / cit / gvsig / gui / command / CommandStackDialog.java @ 11422
History | View | Annotate | Download (9.49 KB)
1 |
package com.iver.cit.gvsig.gui.command; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Color; |
5 |
import java.awt.Component; |
6 |
import java.awt.Dimension; |
7 |
import java.io.IOException; |
8 |
|
9 |
import javax.swing.ImageIcon; |
10 |
import javax.swing.JLabel; |
11 |
import javax.swing.JPanel; |
12 |
import javax.swing.JScrollBar; |
13 |
import javax.swing.JScrollPane; |
14 |
import javax.swing.JSlider; |
15 |
import javax.swing.JTable; |
16 |
import javax.swing.ListSelectionModel; |
17 |
import javax.swing.table.DefaultTableCellRenderer; |
18 |
import javax.swing.table.TableColumn; |
19 |
|
20 |
import org.gvsig.gui.beans.DefaultBean; |
21 |
|
22 |
import com.iver.andami.PluginServices; |
23 |
import com.iver.andami.ui.mdiManager.IWindowListener; |
24 |
import com.iver.andami.ui.mdiManager.SingletonWindow; |
25 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
26 |
import com.iver.cit.gvsig.AddLayer; |
27 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
28 |
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand; |
29 |
import com.iver.cit.gvsig.fmap.edition.commands.CommandListener; |
30 |
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord; |
31 |
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand; |
32 |
|
33 |
public class CommandStackDialog extends DefaultBean implements SingletonWindow, IWindowListener,CommandListener{ |
34 |
|
35 |
private JTable jTable = null; |
36 |
private JPanel jPanel = null; |
37 |
private CommandRecord cr;
|
38 |
private JSlider jSlider = null; |
39 |
//private int itemCount;
|
40 |
private int lowLimit; |
41 |
private int currentValue=-1; |
42 |
private JPanel jPanel1 = null; |
43 |
protected boolean refreshing; |
44 |
private JPanel pCenter = null; |
45 |
private JScrollPane jScrollPane = null; |
46 |
private static final ImageIcon imodify = new ImageIcon(AddLayer.class.getClassLoader() |
47 |
.getResource("images/ModifyCommand.png"));
|
48 |
private static final ImageIcon iadd = new ImageIcon(AddLayer.class.getClassLoader() |
49 |
.getResource("images/AddCommand.png"));
|
50 |
|
51 |
private static final ImageIcon idel = new ImageIcon(AddLayer.class.getClassLoader() |
52 |
.getResource("images/DelCommand.png"));
|
53 |
/**
|
54 |
* This is the default constructor
|
55 |
*/
|
56 |
public CommandStackDialog() {
|
57 |
super();
|
58 |
//this.cr=cr;
|
59 |
//cr.addCommandListener(this);
|
60 |
initialize(); |
61 |
//System.err.println("Identificaci?n del objeto en constructor = "+ cr.toString());
|
62 |
} |
63 |
public void setModel(CommandRecord cr1){ |
64 |
System.err.println("Identificaci?n del objeto en setModel = "+ cr1.toString()); |
65 |
if (this.cr!= null && this.cr.equals(cr1)) |
66 |
return;
|
67 |
this.cr=cr1;
|
68 |
this.cr.addCommandListener(this); |
69 |
initTable(); |
70 |
initSlider(); |
71 |
currentValue=cr.getCommandCount()-cr.getUndoCommands().length; |
72 |
refreshControls(); |
73 |
//refreshSlider(cr.getCommandCount());
|
74 |
refreshScroll(); |
75 |
|
76 |
//
|
77 |
} |
78 |
/**
|
79 |
* This method initializes this
|
80 |
*
|
81 |
* @return void
|
82 |
*/
|
83 |
private void initialize() { |
84 |
this.setLayout(new BorderLayout()); |
85 |
this.setSize(328, 229); |
86 |
this.add(getJPanel(), java.awt.BorderLayout.NORTH);
|
87 |
this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
|
88 |
//refreshScroll();
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* This method initializes jList
|
93 |
*
|
94 |
* @return javax.swing.JList
|
95 |
*/
|
96 |
private JTable getTable() { |
97 |
if (jTable == null) { |
98 |
jTable = new JTable(); |
99 |
} |
100 |
return jTable;
|
101 |
} |
102 |
private void initTable(){ |
103 |
MyModel mymodel=new MyModel(cr);
|
104 |
jTable.setModel(mymodel); |
105 |
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
106 |
jTable.setSelectionBackground(Color.orange);
|
107 |
jTable.setSelectionForeground(Color.black);
|
108 |
jTable.setShowGrid(false);
|
109 |
jTable.getTableHeader().setBackground(Color.white);
|
110 |
TableColumn tc = jTable.getColumnModel().getColumn(0); |
111 |
tc.setCellRenderer(new DefaultTableCellRenderer() { |
112 |
public Component getTableCellRendererComponent(JTable table, |
113 |
Object value,
|
114 |
boolean isSelected,
|
115 |
boolean hasFocus,
|
116 |
int row,
|
117 |
int column)
|
118 |
{ |
119 |
JLabel label = (JLabel) |
120 |
super.getTableCellRendererComponent
|
121 |
(table, value, isSelected, hasFocus, row, column); |
122 |
if (value instanceof AddRowCommand){ |
123 |
label.setIcon(iadd); |
124 |
}else if (value instanceof RemoveRowCommand){ |
125 |
label.setIcon(idel); |
126 |
}else{
|
127 |
label.setIcon(imodify); |
128 |
} |
129 |
if (CommandStackDialog.this.cr.getPos()<row){
|
130 |
label.setBackground(Color.lightGray);
|
131 |
}else {
|
132 |
/* if (cr.getPos()==row){
|
133 |
label.setBackground(Color.orange);
|
134 |
}else{*/
|
135 |
label.setBackground(Color.orange);
|
136 |
//}
|
137 |
} |
138 |
return label;
|
139 |
} |
140 |
}); |
141 |
|
142 |
jTable.addMouseListener(new java.awt.event.MouseAdapter() {
|
143 |
public void mousePressed(java.awt.event.MouseEvent e) { |
144 |
int newpos=jTable.getSelectedRow();
|
145 |
try {
|
146 |
CommandStackDialog.this.cr.setPos(newpos); |
147 |
PluginServices.getMainFrame().enableControls(); |
148 |
} catch (DriverIOException e1) {
|
149 |
e1.printStackTrace(); |
150 |
} catch (IOException e1) { |
151 |
e1.printStackTrace(); |
152 |
} |
153 |
} |
154 |
}); |
155 |
} |
156 |
/**
|
157 |
* This method initializes jPanel
|
158 |
*
|
159 |
* @return javax.swing.JPanel
|
160 |
*/
|
161 |
private JPanel getJPanel() { |
162 |
if (jPanel == null) { |
163 |
jPanel = new JPanel(); |
164 |
} |
165 |
return jPanel;
|
166 |
} |
167 |
|
168 |
public WindowInfo getWindowInfo() {
|
169 |
WindowInfo m_viewinfo = new WindowInfo(WindowInfo.ICONIFIABLE |
|
170 |
WindowInfo.MODELESSDIALOG | WindowInfo.RESIZABLE | WindowInfo.PALETTE); |
171 |
m_viewinfo.setTitle(PluginServices.getText(this,
|
172 |
"pila_de_comandos"));
|
173 |
return m_viewinfo;
|
174 |
} |
175 |
|
176 |
public Object getWindowModel() { |
177 |
return "CommandStack"; |
178 |
} |
179 |
|
180 |
public void windowActivated() { |
181 |
this.validateTree();
|
182 |
} |
183 |
|
184 |
public void windowClosed() { |
185 |
// TODO Auto-generated method stub
|
186 |
|
187 |
} |
188 |
|
189 |
/* (non-Javadoc)
|
190 |
* @see com.iver.cit.gvsig.fmap.edition.commands.CommandListener#executeCommand(com.iver.cit.gvsig.fmap.edition.commands.CommandEvent)
|
191 |
*/
|
192 |
public void commandRepaint() { |
193 |
setValue(cr.getCommandCount()-1-cr.getPos(),true); |
194 |
refreshScroll(); |
195 |
} |
196 |
private void refreshScroll(){ |
197 |
Dimension size=new Dimension(jSlider.getPreferredSize().width,(int)((getTable().getRowCount())*getTable().getRowHeight())); |
198 |
JScrollBar verticalScrollBar=getJScrollPane().getVerticalScrollBar();//ove(size.width,size.height); |
199 |
verticalScrollBar.setValue(cr.getPos()*getTable().getRowHeight()); |
200 |
jSlider.setPreferredSize(size); |
201 |
jSlider.setSize(size); |
202 |
validateTree(); |
203 |
} |
204 |
/**
|
205 |
* This method initializes jSlider
|
206 |
*
|
207 |
* @return javax.swing.JSlider
|
208 |
*/
|
209 |
private JSlider getJSlider() { |
210 |
if (jSlider == null) { |
211 |
jSlider = new JSlider(); |
212 |
} |
213 |
return jSlider;
|
214 |
} |
215 |
private void initSlider(){ |
216 |
jSlider.setOrientation(javax.swing.JSlider.VERTICAL); |
217 |
jSlider.setPreferredSize(new Dimension(jSlider.getPreferredSize().width,(int)((getTable().getRowCount())*getTable().getRowHeight()))); |
218 |
jSlider.addMouseListener(new java.awt.event.MouseAdapter() {
|
219 |
public void mouseReleased(java.awt.event.MouseEvent e) { |
220 |
|
221 |
} |
222 |
}); |
223 |
jSlider.addChangeListener(new javax.swing.event.ChangeListener() {
|
224 |
|
225 |
|
226 |
public void stateChanged(javax.swing.event.ChangeEvent e) { |
227 |
int value = (int) (getJSlider().getValue() * cr.getCommandCount() * 0.01); |
228 |
try {
|
229 |
if (!refreshing)
|
230 |
cr.setPos(cr.getCommandCount()-1-value);
|
231 |
//System.out.println("setPos = "+(cr.getCommandCount()-1-value));
|
232 |
} catch (DriverIOException e1) {
|
233 |
e1.printStackTrace(); |
234 |
} catch (IOException e1) { |
235 |
e1.printStackTrace(); |
236 |
} |
237 |
|
238 |
} |
239 |
}); |
240 |
setValue(cr.getCommandCount()-1-cr.getPos(),true); |
241 |
} |
242 |
public void setValue(int number, boolean fireEvent) { |
243 |
if (number < lowLimit)
|
244 |
number = lowLimit; |
245 |
if (number > cr.getCommandCount())
|
246 |
number = cr.getCommandCount(); |
247 |
if (number != currentValue) {
|
248 |
currentValue = number; |
249 |
refreshControls(); |
250 |
if (fireEvent)
|
251 |
callValueChanged(new Integer(currentValue)); |
252 |
} |
253 |
int selpos=cr.getCommandCount()-1-number; |
254 |
if (selpos>=0){ |
255 |
getTable().setRowSelectionInterval(selpos,selpos); |
256 |
} else
|
257 |
getTable().clearSelection(); |
258 |
} |
259 |
/**
|
260 |
* Refreshes all the mutable controls in this component.
|
261 |
*/
|
262 |
private void refreshControls() { |
263 |
int normalizedValue = (int) ((currentValue / (float) cr.getCommandCount())*100); |
264 |
refreshSlider(normalizedValue); |
265 |
} |
266 |
/**
|
267 |
* Sets the slider to the correct (scaled) position.
|
268 |
* @param normalizedValue
|
269 |
*/
|
270 |
private void refreshSlider(int normalizedValue) { |
271 |
refreshing = true;
|
272 |
getJSlider().setValue(normalizedValue); |
273 |
refreshing = false;
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* This method initializes jPanel1
|
278 |
*
|
279 |
* @return javax.swing.JPanel
|
280 |
*/
|
281 |
private JPanel getJPanel1() { |
282 |
if (jPanel1 == null) { |
283 |
jPanel1 = new JPanel(); |
284 |
jPanel1.add(getJSlider()); |
285 |
} |
286 |
return jPanel1;
|
287 |
} |
288 |
|
289 |
|
290 |
|
291 |
/**
|
292 |
* This method initializes pCenter
|
293 |
*
|
294 |
* @return javax.swing.JPanel
|
295 |
*/
|
296 |
private JPanel getPCenter() { |
297 |
if (pCenter == null) { |
298 |
pCenter = new JPanel(); |
299 |
pCenter.setLayout(new BorderLayout()); |
300 |
pCenter.add(getTable(), java.awt.BorderLayout.CENTER); |
301 |
pCenter.add(getJPanel1(), java.awt.BorderLayout.WEST); |
302 |
} |
303 |
return pCenter;
|
304 |
} |
305 |
|
306 |
/**
|
307 |
* This method initializes jScrollPane
|
308 |
*
|
309 |
* @return javax.swing.JScrollPane
|
310 |
*/
|
311 |
private JScrollPane getJScrollPane() { |
312 |
if (jScrollPane == null) { |
313 |
jScrollPane = new JScrollPane(); |
314 |
jScrollPane.setViewportView(getPCenter()); |
315 |
} |
316 |
return jScrollPane;
|
317 |
} |
318 |
|
319 |
public void commandRefresh() { |
320 |
commandRepaint(); |
321 |
|
322 |
} |
323 |
} // @jve:decl-index=0:visual-constraint="10,10"
|