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