svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / gui / projectpanel / ProjectDocumentsPanelPage.java @ 46033
History | View | Annotate | Download (19.2 KB)
1 |
package org.gvsig.app.project.documents.gui.projectpanel; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import org.gvsig.app.project.documents.gui.*; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.FlowLayout; |
7 |
import java.awt.event.ActionEvent; |
8 |
import java.awt.event.ActionListener; |
9 |
import java.awt.event.ComponentAdapter; |
10 |
import java.awt.event.ComponentEvent; |
11 |
import java.awt.event.MouseAdapter; |
12 |
import java.awt.event.MouseEvent; |
13 |
import java.beans.PropertyChangeEvent; |
14 |
import java.beans.PropertyChangeListener; |
15 |
import java.io.File; |
16 |
import java.util.Collections; |
17 |
import java.util.Comparator; |
18 |
import java.util.Iterator; |
19 |
import java.util.List; |
20 |
import javax.swing.ButtonGroup; |
21 |
import javax.swing.JComponent; |
22 |
import javax.swing.JLabel; |
23 |
import javax.swing.JList; |
24 |
import javax.swing.JOptionPane; |
25 |
import javax.swing.JPanel; |
26 |
import javax.swing.JRadioButton; |
27 |
import javax.swing.JScrollPane; |
28 |
import javax.swing.ScrollPaneConstants; |
29 |
import javax.swing.SwingUtilities; |
30 |
import javax.swing.event.ListSelectionEvent; |
31 |
import javax.swing.event.ListSelectionListener; |
32 |
import org.apache.commons.lang3.StringUtils; |
33 |
import org.gvsig.andami.PluginServices; |
34 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
35 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
36 |
import org.gvsig.app.ApplicationLocator; |
37 |
import org.gvsig.app.ApplicationManager; |
38 |
import org.gvsig.app.project.Project; |
39 |
import org.gvsig.app.project.ProjectManager; |
40 |
import org.gvsig.app.project.documents.Document; |
41 |
import org.gvsig.app.project.documents.DocumentManager; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.i18n.I18nManager; |
44 |
import org.gvsig.tools.identitymanagement.SimpleIdentityManager; |
45 |
import org.gvsig.tools.observer.Observable; |
46 |
import org.gvsig.tools.observer.Observer; |
47 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
49 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
50 |
import org.gvsig.tools.util.Invocable; |
51 |
import org.gvsig.utils.DefaultListModel; |
52 |
import org.slf4j.Logger; |
53 |
import org.slf4j.LoggerFactory; |
54 |
|
55 |
public class ProjectDocumentsPanelPage |
56 |
extends ProjectDocumentsPanelPageView
|
57 |
implements ProjectPanelPage {
|
58 |
|
59 |
private final Logger LOG = LoggerFactory.getLogger(ProjectGeneralPropertiesPage.class); |
60 |
|
61 |
private Project project;
|
62 |
private DocumentManager currentDocumentType;
|
63 |
private final PropertyChangeListener projectPropertyChangeListener; |
64 |
private final Observer projectObserver; |
65 |
|
66 |
public ProjectDocumentsPanelPage() {
|
67 |
this.project = null; |
68 |
this.projectPropertyChangeListener = new PropertyChangeListener() { |
69 |
@Override
|
70 |
public void propertyChange(PropertyChangeEvent evt) { |
71 |
updateProject(); |
72 |
} |
73 |
}; |
74 |
this.projectObserver = new Observer() { |
75 |
@Override
|
76 |
public void update(Observable observable, Object notification) { |
77 |
updateProject(); |
78 |
} |
79 |
}; |
80 |
this.initComponents();
|
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void setData(Object data) { |
85 |
} |
86 |
|
87 |
@Override
|
88 |
public String getTitle() { |
89 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
90 |
return i18n.getTranslation("_Documents"); |
91 |
} |
92 |
|
93 |
@Override
|
94 |
public int getPriority() { |
95 |
return 1000; |
96 |
} |
97 |
|
98 |
@Override
|
99 |
public boolean whenAccept() { |
100 |
return true; |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public boolean whenApply() { |
105 |
return true; |
106 |
} |
107 |
|
108 |
@Override
|
109 |
public boolean whenCancel() { |
110 |
return true; |
111 |
} |
112 |
|
113 |
@Override
|
114 |
public JComponent asJComponent() { |
115 |
return this; |
116 |
} |
117 |
|
118 |
private void initComponents() { |
119 |
this.initDocumentTypes();
|
120 |
this.btnDocumentNew.addActionListener(new ActionListener() { |
121 |
@Override
|
122 |
public void actionPerformed(ActionEvent e) { |
123 |
doDocumentNew(); |
124 |
} |
125 |
}); |
126 |
this.btnDocumentOpen.addActionListener(new ActionListener() { |
127 |
@Override
|
128 |
public void actionPerformed(ActionEvent e) { |
129 |
doDocumentOpen(); |
130 |
} |
131 |
}); |
132 |
this.btnDocumentRename.addActionListener(new ActionListener() { |
133 |
@Override
|
134 |
public void actionPerformed(ActionEvent e) { |
135 |
doDocumentRename(); |
136 |
} |
137 |
}); |
138 |
this.btnDocumentDelete.addActionListener(new ActionListener() { |
139 |
@Override
|
140 |
public void actionPerformed(ActionEvent e) { |
141 |
doDocumentDelete(); |
142 |
} |
143 |
}); |
144 |
this.btnDocumentProperties.addActionListener(new ActionListener() { |
145 |
@Override
|
146 |
public void actionPerformed(ActionEvent e) { |
147 |
doDocumentProperties(); |
148 |
} |
149 |
}); |
150 |
this.btnProjectProperties.addActionListener(new ActionListener() { |
151 |
@Override
|
152 |
public void actionPerformed(ActionEvent e) { |
153 |
doProjectProperties(); |
154 |
} |
155 |
}); |
156 |
this.lstDocuments.addListSelectionListener(new ListSelectionListener() { |
157 |
@Override
|
158 |
public void valueChanged(ListSelectionEvent e) { |
159 |
if (e.getValueIsAdjusting()) {
|
160 |
return;
|
161 |
} |
162 |
doDocumentsSelectionChanged(); |
163 |
} |
164 |
}); |
165 |
this.lstDocuments.addMouseListener(new MouseAdapter() { |
166 |
@Override
|
167 |
public void mouseClicked(MouseEvent e) { |
168 |
if( e.getButton()==MouseEvent.BUTTON1 && e.getClickCount()==2 ) { |
169 |
doDocumentOpen(); |
170 |
} |
171 |
} |
172 |
}); |
173 |
this.translate();
|
174 |
} |
175 |
|
176 |
private void translate() { |
177 |
ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager(); |
178 |
manager.translate(this.lblCreationDate);
|
179 |
manager.translate(this.lblSavedIn);
|
180 |
manager.translate(this.lblSessionName);
|
181 |
manager.translate(this.lblDocument);
|
182 |
manager.translate(this.lblDocumentTypes);
|
183 |
manager.translate(this.lblSessionProperties);
|
184 |
manager.translate(this.lblCreationDate);
|
185 |
manager.translate(this.lblCreationDate);
|
186 |
manager.translate(this.lblCreationDate);
|
187 |
manager.translate(this.lblCreationDate);
|
188 |
manager.translate(this.lblCreationDate);
|
189 |
manager.translate(this.lblCreationDate);
|
190 |
|
191 |
manager.translate(this.btnDocumentDelete);
|
192 |
manager.translate(this.btnDocumentNew);
|
193 |
manager.translate(this.btnDocumentOpen);
|
194 |
manager.translate(this.btnDocumentProperties);
|
195 |
manager.translate(this.btnDocumentRename);
|
196 |
manager.translate(this.btnProjectProperties);
|
197 |
|
198 |
} |
199 |
|
200 |
@Override
|
201 |
public void setProject(Project project) { |
202 |
if( this.project!=null ) { |
203 |
this.project.removePropertyChangeListener(this.projectPropertyChangeListener); |
204 |
this.project.deleteObserver(this.projectObserver); |
205 |
} |
206 |
this.project = project;
|
207 |
if(project!=null){ |
208 |
project.addPropertyChangeListener(this.projectPropertyChangeListener);
|
209 |
project.addObserver(this.projectObserver);
|
210 |
} |
211 |
updateProject(); |
212 |
} |
213 |
|
214 |
private void updateProject() { |
215 |
if( !SwingUtilities.isEventDispatchThread() ) { |
216 |
SwingUtilities.invokeLater(new Runnable() { |
217 |
@Override
|
218 |
public void run() { |
219 |
updateProject(); |
220 |
} |
221 |
}); |
222 |
return;
|
223 |
} |
224 |
updateProjectDocumens(); |
225 |
updateProjectProperties(); |
226 |
} |
227 |
|
228 |
private void updateProjectProperties() { |
229 |
if (project != null) { |
230 |
this.txtSessionName.setText(project.getName());
|
231 |
File path = this.project.getFile(); |
232 |
if (path != null) { |
233 |
this.txtSavedIn.setText(path.toString());
|
234 |
} else {
|
235 |
this.txtSavedIn.setText(""); |
236 |
} |
237 |
this.txtCreationDate.setText(project.getCreationDate());
|
238 |
} |
239 |
} |
240 |
|
241 |
private void initDocumentTypes() { |
242 |
SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager(); |
243 |
|
244 |
this.documentTypesContainer.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); |
245 |
|
246 |
List<DocumentManager> factories = ProjectManager.getInstance().getDocumentManagers();
|
247 |
Collections.sort(factories, new Comparator<DocumentManager>() { |
248 |
|
249 |
@Override
|
250 |
public int compare(DocumentManager arg0, DocumentManager arg1) { |
251 |
return arg0.getPriority() - arg1.getPriority();
|
252 |
} |
253 |
|
254 |
}); |
255 |
this.currentDocumentType = factories.get(0); |
256 |
JRadioButton selectButton = null; |
257 |
ButtonGroup group = new ButtonGroup(); |
258 |
JPanel panel = new JPanel(); |
259 |
for (DocumentManager documentFactory : factories) {
|
260 |
if (!identityManager.getCurrentIdentity().isAuthorized(
|
261 |
Document.ACCESS_DOCUMENT_AUTHORIZATION,
|
262 |
documentFactory, |
263 |
documentFactory.getTypeName())) { |
264 |
continue;
|
265 |
} |
266 |
JRadioButton button = this.createDocumentTypeButton(documentFactory); |
267 |
group.add(button); |
268 |
panel.add(button); |
269 |
if( documentFactory == this.currentDocumentType ) { |
270 |
selectButton = button; |
271 |
} |
272 |
} |
273 |
|
274 |
JScrollPane scroll = new JScrollPane(panel); |
275 |
Dimension dim = panel.getPreferredSize();
|
276 |
scroll.setMinimumSize(new Dimension(dim.width+2, dim.height + 35)); |
277 |
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
|
278 |
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
279 |
|
280 |
this.documentTypesContainer.setLayout(new BorderLayout()); |
281 |
this.documentTypesContainer.add(scroll, BorderLayout.CENTER); |
282 |
selectButton.setSelected(true);
|
283 |
} |
284 |
|
285 |
private JRadioButton createDocumentTypeButton(final DocumentManager documentFactory) { |
286 |
final JRadioButton rb = new JRadioButton(); |
287 |
|
288 |
rb.setIcon(documentFactory.getIcon()); |
289 |
rb.setSelectedIcon(documentFactory.getIconSelected()); |
290 |
rb.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); |
291 |
rb.setText(documentFactory.getTitle()); |
292 |
rb.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); |
293 |
rb.setName(documentFactory.getTypeName()); |
294 |
rb.addChangeListener(new javax.swing.event.ChangeListener() {
|
295 |
@Override
|
296 |
public void stateChanged(javax.swing.event.ChangeEvent e) { |
297 |
if (rb.isSelected()) {
|
298 |
currentDocumentType = documentFactory; |
299 |
updateProjectDocumens(); |
300 |
} |
301 |
} |
302 |
}); |
303 |
return rb;
|
304 |
} |
305 |
|
306 |
private void openDocumentWindow(Document doc) { |
307 |
if (doc == null) { |
308 |
return;
|
309 |
} |
310 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
311 |
ApplicationManager application = ApplicationLocator.getManager(); |
312 |
IWindow window = doc.getFactory().getMainWindow(doc); |
313 |
if (window == null) { |
314 |
application.message( |
315 |
i18n.getTranslation("error_opening_the_document"),
|
316 |
JOptionPane.WARNING_MESSAGE
|
317 |
); |
318 |
return;
|
319 |
} |
320 |
application.getUIManager().addWindow( |
321 |
window, |
322 |
MDIManager.ALIGN_FIRST_LINE_END_CASCADE |
323 |
); |
324 |
project.setModified(true);
|
325 |
} |
326 |
|
327 |
private void updateProjectDocumens() { |
328 |
if (project == null) { |
329 |
return;
|
330 |
} |
331 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
332 |
this.lblDocument.setText(i18n.getTranslation("documentos_existentes")); |
333 |
|
334 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
335 |
List<Document> documents = project.getDocuments(documentsTypeName); |
336 |
DefaultListModel model = new DefaultListModel(documents); |
337 |
this.lstDocuments.setModel(model);
|
338 |
this.lblDocument.setText(currentDocumentType.getTitle());
|
339 |
updateDocumentButtons(); |
340 |
} |
341 |
|
342 |
private void updateDocumentButtons() { |
343 |
this.btnDocumentNew.setEnabled(true); |
344 |
if (this.lstDocuments.getSelectedIndex() != -1) { |
345 |
this.btnDocumentOpen.setEnabled(true); |
346 |
this.btnDocumentDelete.setEnabled(true); |
347 |
this.btnDocumentRename.setEnabled(true); |
348 |
this.btnDocumentProperties.setEnabled(true); |
349 |
} else {
|
350 |
btnDocumentOpen.setEnabled(false);
|
351 |
btnDocumentDelete.setEnabled(false);
|
352 |
btnDocumentRename.setEnabled(false);
|
353 |
btnDocumentProperties.setEnabled(false);
|
354 |
} |
355 |
} |
356 |
|
357 |
private void doDocumentNew() { |
358 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
359 |
ProjectManager projectManager = ProjectManager.getInstance(); |
360 |
Iterator<? extends Document> documents = projectManager.createDocumentsByUser(documentsTypeName, new Invocable() { |
361 |
@Override
|
362 |
public Object call(Object... args) { |
363 |
Iterator<? extends Document> docs = (Iterator<? extends Document>) args[0]; |
364 |
addNewDocuments(docs); |
365 |
return null; |
366 |
} |
367 |
}); |
368 |
addNewDocuments(documents); |
369 |
} |
370 |
|
371 |
private void addNewDocuments(Iterator<? extends Document> documents) { |
372 |
while ( documents != null && documents.hasNext() ) { |
373 |
try {
|
374 |
Document document = documents.next();
|
375 |
project.addDocument(document); |
376 |
openDocumentWindow(document); |
377 |
} catch (Exception e) { |
378 |
LOG.warn("Can't open document", e);
|
379 |
} |
380 |
} |
381 |
this.lstDocuments.requestFocus();
|
382 |
this.lstDocuments.setSelectedIndex(this.lstDocuments.getModel().getSize()-1); |
383 |
} |
384 |
|
385 |
private void doDocumentOpen() { |
386 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
387 |
ApplicationManager application = ApplicationLocator.getManager(); |
388 |
|
389 |
int[] indexes = this.lstDocuments.getSelectedIndices(); |
390 |
for (int i = indexes.length - 1; i >= 0; i--) { |
391 |
int index = indexes[i];
|
392 |
if (index == -1) { |
393 |
return;
|
394 |
} |
395 |
String documentsTypeName = this.currentDocumentType.getTypeName(); |
396 |
List<Document> documents = project.getDocuments(documentsTypeName); |
397 |
Document doc = documents.get(index);
|
398 |
if (!doc.isAvailable()) {
|
399 |
application.messageDialog( |
400 |
i18n.getTranslation("document_not_available"),
|
401 |
i18n.getTranslation("abrir"),
|
402 |
JOptionPane.WARNING_MESSAGE
|
403 |
); |
404 |
} |
405 |
IWindow win = doc.getMainWindow(); |
406 |
openDocumentWindow(doc); |
407 |
} |
408 |
|
409 |
} |
410 |
|
411 |
private void doDocumentRename() { |
412 |
ApplicationManager application = ApplicationLocator.getManager(); |
413 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
414 |
|
415 |
int index = this.lstDocuments.getSelectedIndex(); |
416 |
if (index == -1) { |
417 |
return;
|
418 |
} |
419 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
420 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
421 |
Document doc = documents.get(index);
|
422 |
String name = application.inputDialog(
|
423 |
i18n.getTranslation("introduce_nombre"),
|
424 |
i18n.getTranslation("renombrar"),
|
425 |
JOptionPane.QUESTION_MESSAGE,
|
426 |
doc.getName() |
427 |
); |
428 |
if(name != null) { |
429 |
for (Document document : documents) { |
430 |
if (StringUtils.equals(document.getName(), name)) {
|
431 |
application.messageDialog( |
432 |
i18n.getTranslation("elemento_ya_existe"),
|
433 |
i18n.getTranslation("warning"),
|
434 |
JOptionPane.WARNING_MESSAGE
|
435 |
); |
436 |
return;
|
437 |
} |
438 |
} |
439 |
doc.setName(name); |
440 |
updateProjectDocumens(); |
441 |
this.project.setModified(true); |
442 |
} |
443 |
} |
444 |
|
445 |
private void doDocumentDelete() { |
446 |
ApplicationManager application = ApplicationLocator.getManager(); |
447 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
448 |
|
449 |
int res = application.confirmDialog(
|
450 |
i18n.getTranslation("confirmar_borrar"),
|
451 |
i18n.getTranslation("borrar"),
|
452 |
JOptionPane.YES_NO_OPTION,
|
453 |
JOptionPane.INFORMATION_MESSAGE,
|
454 |
"Confirm remove document from project"
|
455 |
); |
456 |
int lastremoved = 0; |
457 |
if (res == JOptionPane.YES_OPTION) { |
458 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
459 |
int[] indexes = this.lstDocuments.getSelectedIndices(); |
460 |
for (int i = indexes.length - 1; i >= 0; i--) { |
461 |
int index = indexes[i];
|
462 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
463 |
Document doc = documents.get(index);
|
464 |
if (doc.isLocked()) {
|
465 |
application.messageDialog( |
466 |
i18n.getTranslation("locked_element_it_cannot_be_deleted"),
|
467 |
i18n.getTranslation("Information"),
|
468 |
JOptionPane.INFORMATION_MESSAGE
|
469 |
); |
470 |
return;
|
471 |
} |
472 |
PluginServices.getMDIManager().closeSingletonWindow(doc); |
473 |
project.removeDocument(doc); |
474 |
lastremoved = index; |
475 |
} |
476 |
if (lastremoved >= this.lstDocuments.getModel().getSize()) { |
477 |
this.lstDocuments.setSelectedIndex(this.lstDocuments.getModel().getSize() - 1); |
478 |
} else if (lastremoved >= 0) { |
479 |
this.lstDocuments.setSelectedIndex(lastremoved);
|
480 |
} |
481 |
project.setModified(true);
|
482 |
} |
483 |
|
484 |
} |
485 |
|
486 |
private void doDocumentProperties() { |
487 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
488 |
ApplicationManager application = ApplicationLocator.getManager(); |
489 |
|
490 |
int index = this.lstDocuments.getSelectedIndex(); |
491 |
if (index == -1) { |
492 |
return;
|
493 |
} |
494 |
String DoumentTypeName = this.currentDocumentType.getTypeName(); |
495 |
List<Document> documents = project.getDocuments(DoumentTypeName); |
496 |
Document doc = documents.get(index);
|
497 |
if (!doc.isAvailable()) {
|
498 |
application.messageDialog( |
499 |
i18nManager.getTranslation("document_not_available"),
|
500 |
i18nManager.getTranslation("properties"),
|
501 |
JOptionPane.WARNING_MESSAGE
|
502 |
); |
503 |
return;
|
504 |
} |
505 |
IWindow properties = doc.getFactory().getPropertiesWindow(doc); |
506 |
application.getUIManager().addWindow( |
507 |
properties |
508 |
); |
509 |
this.lstDocuments.setSelectedIndex(index);
|
510 |
project.setModified(true);
|
511 |
} |
512 |
|
513 |
private void doProjectProperties() { |
514 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
515 |
WindowManager winmgr = ToolsSwingLocator.getWindowManager(); |
516 |
ProjectProperties panel = new ProjectProperties(project);
|
517 |
panel.addComponentListener(new ComponentAdapter() { |
518 |
@Override
|
519 |
public void componentHidden(ComponentEvent e) { |
520 |
updateProjectProperties(); |
521 |
} |
522 |
}); |
523 |
winmgr.showWindow( |
524 |
panel, |
525 |
i18nManager.getTranslation("project_properties"),
|
526 |
WindowManager.MODE.WINDOW |
527 |
); |
528 |
} |
529 |
|
530 |
private void doDocumentsSelectionChanged() { |
531 |
this.updateDocumentButtons();
|
532 |
} |
533 |
} |