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