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