Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / wizard / UnsavedDataPanel.java @ 40978

History | View | Annotate | Download (17.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.andami.ui.wizard;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.FlowLayout;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.GridLayout;
33
import java.awt.Insets;
34
import java.awt.event.ActionEvent;
35
import java.awt.event.ActionListener;
36
import java.awt.event.MouseEvent;
37
import java.awt.event.MouseListener;
38
import java.awt.image.BufferedImage;
39
import java.util.ArrayList;
40
import java.util.Iterator;
41

    
42
import javax.swing.BorderFactory;
43
import javax.swing.ImageIcon;
44
import javax.swing.JCheckBox;
45
import javax.swing.JLabel;
46
import javax.swing.JPanel;
47
import javax.swing.JScrollPane;
48

    
49
import org.gvsig.andami.IconThemeHelper;
50
import org.gvsig.andami.PluginServices;
51
import org.gvsig.andami.plugins.status.IUnsavedData;
52
import org.gvsig.andami.ui.mdiManager.IWindow;
53
import org.gvsig.andami.ui.mdiManager.WindowInfo;
54
import org.gvsig.gui.beans.swing.JButton;
55

    
56

    
57
/**
58
 * Ask for elements to save before application exit
59
 *
60
 *
61
 */
62
public class UnsavedDataPanel extends JPanel implements IWindow, ActionListener {
63
        /**
64
         *
65
         */
66
        private static final long serialVersionUID = -4745219917358767905L;
67

    
68

    
69
        private static ImageIcon blanckIcon = null;
70
        private JPanel pResources = null;
71
        private JPanel pButtons = null;
72
        private JScrollPane pScrollList = null;
73
        private myList list = null;
74
        private JLabel lblDescription = null;
75
        private JButton botSave = null;
76
        private JButton botDiscard = null;
77
        private JButton botDontExit = null;
78
        private JPanel pActionButtons = null;
79
        private JPanel pSelectionButtons = null;
80
        private JButton botSelectAll = null;
81
        private JButton botDeselectAll = null;
82

    
83
        private ArrayList listeners = new ArrayList();
84
        private JLabel lblResourceDescription = null;
85
        private IUnsavedData[] unsavedDataList;
86
        private String windowTitle = PluginServices.getText(this, "save_resources");
87

    
88

    
89

    
90
        /**
91
         * This is the default constructor
92
         */
93
        public UnsavedDataPanel(IUnsavedData[] unsavedDatalist) {
94
                super();
95
                initialize();
96
                this.setUnsavedDataArray(unsavedDatalist);
97
        }
98

    
99
        /**
100
         * This method initializes this
101
         *
102
         * @return void
103
         */
104
        private void initialize() {
105
                BorderLayout borderLayout = new BorderLayout();
106
                borderLayout.setHgap(5);
107
                borderLayout.setVgap(5);
108
                lblDescription = new JLabel();
109
                lblDescription.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
110
                lblDescription.setText(PluginServices.getText(this, "select_resources_to_save_before_exit"));
111
                lblDescription.setPreferredSize(new Dimension(497, 30));
112
                lblDescription.setName("lblDescription");
113
                this.setLayout(borderLayout);
114
                this.setSize(396, 272);
115
                this.add(getPResources(), java.awt.BorderLayout.CENTER);
116
                this.add(getPButtons(), java.awt.BorderLayout.SOUTH);
117
                this.add(lblDescription, BorderLayout.NORTH);
118
        }
119

    
120
        /**
121
         * This method initializes pResources
122
         *
123
         * @return javax.swing.JPanel
124
         */
125
        private JPanel getPResources() {
126
                if (pResources == null) {
127
                        pResources = new JPanel();
128
                        pResources.setLayout(new BorderLayout());
129
                        pResources.add(getPScrollList(), BorderLayout.CENTER);
130
                        // pResources.add(getLblResourceDescription(), BorderLayout.SOUTH);
131
                }
132
                return pResources;
133
        }
134

    
135

    
136
        /**
137
         * This method initializes lblResourceDescription
138
         *
139
         * @return javax.swing.JLabel
140
         */
141
        private JLabel getLblResourceDescription() {
142
                if (lblResourceDescription == null) {
143
                        lblResourceDescription = new JLabel();
144
                        lblResourceDescription.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
145
                        lblResourceDescription.setText("");
146
                        lblResourceDescription.setPreferredSize(new Dimension(38, 50));
147
                        lblResourceDescription.setName("lblResourceDescription");
148
                }
149
                return lblResourceDescription;
150
        }
151

    
152

    
153
        /**
154
         * This method initializes pButtons
155
         *
156
         * @return javax.swing.JPanel
157
         */
158
        private JPanel getPButtons() {
159
                if (pButtons == null) {
160
                        BorderLayout borderLayout2 = new BorderLayout();
161
                        borderLayout2.setHgap(5);
162
                        borderLayout2.setVgap(5);
163
                        pButtons = new JPanel();
164
                        pButtons.setLayout(borderLayout2);
165
                        pButtons.add(getPSelectionButtons(), BorderLayout.NORTH);
166
                        pButtons.add(getPActionButtons(), BorderLayout.SOUTH);
167
                }
168
                return pButtons;
169
        }
170

    
171
        /**
172
         * This method initializes pScrollList
173
         *
174
         * @return javax.swing.JScrollPane
175
         */
176
        private JScrollPane getPScrollList() {
177
                if (pScrollList == null) {
178
                        pScrollList = new JScrollPane();
179
                        pScrollList.setPreferredSize(new java.awt.Dimension(350,350));
180
                        pScrollList.setViewportView(getList());
181
                }
182
                return pScrollList;
183
        }
184

    
185
        /**
186
         * This method initializes list
187
         *
188
         * @return javax.swing.JList
189
         */
190
        private myList getList() {
191
                if (list == null) {
192
                        list = new myList();
193
                        list.setBackground(Color.white);
194
                }
195
                return list;
196
        }
197

    
198
        /**
199
         * This method initializes botSave
200
         *
201
         * @return javax.swing.JButton
202
         */
203
        private JButton getBotSave() {
204
                if (botSave == null) {
205
                        botSave = new JButton();
206
                        botSave.setName("botSave");
207
                        botSave.setToolTipText(PluginServices.getText(this, "save_selected_resources_and_exit"));
208
                        botSave.setText(PluginServices.getText(this, "ok"));
209
                        botSave.setActionCommand("Accept");
210
                        botSave.addActionListener(this);
211
                }
212
                return botSave;
213
        }
214

    
215
        /**
216
         * This method initializes botDiscard
217
         *
218
         * @return javax.swing.JButton
219
         */
220
        private JButton getBotDiscard() {
221
                if (botDiscard == null) {
222
                        botDiscard = new JButton();
223
                        botDiscard.setName("botDiscard");
224
                        botDiscard.setToolTipText(PluginServices.getText(this, "discard_changes_and_exit"));
225
                        botDiscard.setText(PluginServices.getText(this, "discard_changes"));
226
                        botDiscard.setActionCommand("Discard");
227
                        botDiscard.addActionListener(this);
228
                }
229
                return botDiscard;
230
        }
231

    
232
        /**
233
         * This method initializes botDontExit
234
         *
235
         * @return javax.swing.JButton
236
         */
237
        private JButton getBotDontExit() {
238
                if (botDontExit == null) {
239
                        botDontExit = new JButton();
240
                        botDontExit.setName("botDontExit");
241
                        botDontExit.setToolTipText(PluginServices.getText(this, "cancel_the_application_termination"));
242
                        botDontExit.setText(PluginServices.getText(this, "cancelar"));
243
                        botDontExit.setActionCommand("Cancel");
244
                        botDontExit.addActionListener(this);
245
                }
246
                return botDontExit;
247
        }
248

    
249
        /**
250
         * This method initializes pActionButtons
251
         *
252
         * @return javax.swing.JPanel
253
         */
254
        private JPanel getPActionButtons() {
255
                if (pActionButtons == null) {
256
                    
257
                    GridLayout gl = new GridLayout(1, 3, 8, 0);
258
                    /*
259
                        FlowLayout flowLayout = new FlowLayout();
260
                        flowLayout.setAlignment(FlowLayout.RIGHT);
261
                        */
262
                        pActionButtons = new JPanel();
263
                        pActionButtons.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
264
                        pActionButtons.setLayout(gl);
265
                        pActionButtons.setName("pActionButtons");
266
                        pActionButtons.add(getBotSave());
267
                        pActionButtons.add(getBotDiscard());
268
                        pActionButtons.add(getBotDontExit());
269
                }
270
                return pActionButtons;
271
        }
272

    
273
        /**
274
         * This method initializes pSelectionButtons
275
         *
276
         * @return javax.swing.JPanel
277
         */
278
        private JPanel getPSelectionButtons() {
279
                if (pSelectionButtons == null) {
280
                    GridLayout gl = new GridLayout(2, 3, 8, 8);
281
                    /*
282
                        FlowLayout flowLayout1 = new FlowLayout();
283
                        flowLayout1.setAlignment(FlowLayout.LEFT);
284
                        */
285
                        pSelectionButtons = new JPanel();
286
                        pSelectionButtons.setBorder(BorderFactory.createEmptyBorder(5,8,8,8));
287
                        pSelectionButtons.setLayout(gl);
288
                        pSelectionButtons.setName("pSelectionButtons");
289
                        pSelectionButtons.add(getBotSelectAll());
290
                        pSelectionButtons.add(new JLabel());
291
                        pSelectionButtons.add(new JLabel());
292
                        // new row
293
                        pSelectionButtons.add(getBotDeselectAll());
294
                        pSelectionButtons.add(new JLabel());
295
            pSelectionButtons.add(new JLabel());
296
                        
297
                }
298
                return pSelectionButtons;
299
        }
300

    
301
        /**
302
         * This method initializes botSelectAll
303
         *
304
         * @return javax.swing.JButton
305
         */
306
        private JButton getBotSelectAll() {
307
                if (botSelectAll == null) {
308
                        botSelectAll = new JButton();
309
                        botSelectAll.setText(PluginServices.getText(this, "select_all"));
310
                        botSelectAll.setName("botSelectAll");
311
                        botSelectAll.setToolTipText(PluginServices.getText(this, "select_all_resources"));
312
                        botSelectAll.setActionCommand("SelectAll");
313
                        botSelectAll.addActionListener(this);
314
                }
315
                return botSelectAll;
316
        }
317

    
318
        /**
319
         * This method initializes botDeselectAll
320
         *
321
         * @return javax.swing.JButton
322
         */
323
        private JButton getBotDeselectAll() {
324
                if (botDeselectAll == null) {
325
                        botDeselectAll = new JButton();
326
                        botDeselectAll.setName("botDeselectAll");
327
                        botDeselectAll.setToolTipText(PluginServices.getText(this, "deselect_all_resources"));
328
                        botDeselectAll.setText(PluginServices.getText(this, "deselect_all"));
329
                        botDeselectAll.setActionCommand("DeselectAll");
330
                        botDeselectAll.addActionListener(this);
331
                }
332
                return botDeselectAll;
333
        }
334

    
335
        public WindowInfo getWindowInfo() {
336
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG
337
                                | WindowInfo.RESIZABLE
338
                                | WindowInfo.MAXIMIZABLE);
339
                wi.setTitle(windowTitle);
340
                wi.setHeight(270);
341
                wi.setWidth(450);
342
                return wi;
343
        }
344

    
345
        public void setWindowTitle(String windowTitle) {
346
                this.windowTitle = windowTitle;
347
        }
348

    
349
        public void setCancelText(String text, String tooltipText) {
350
                getBotDontExit().setToolTipText(tooltipText);
351
                getBotDontExit().setText(text);
352
        }
353

    
354
        public void setDiscardText(String text, String tooltipText) {
355
                getBotDiscard().setToolTipText(tooltipText);
356
                getBotDiscard().setText(text);
357
        }
358

    
359
        public void setAcceptText(String text, String tooltipText) {
360
                getBotSave().setToolTipText(tooltipText);
361
                getBotSave().setText(text);
362
        }
363

    
364
        public void setHeaderText(String text) {
365
                lblDescription.setText(text);
366
        }
367

    
368
        /**
369
         * Register a listener to call
370
         *
371
         * @param a UnsavedDataPanelListener instance
372
         */
373
        public void addActionListener(UnsavedDataPanelListener listener){
374
                this.listeners.add(listener);
375
        }
376

    
377
        public void removeActionListener(UnsavedDataPanelListener listener){
378
                this.listeners.remove(listener);
379
        }
380

    
381
        public class UnsavedDataPanelListener {
382
                public void cancel(UnsavedDataPanel panel){
383

    
384
                }
385

    
386
                public void accept(UnsavedDataPanel panel){
387

    
388
                }
389

    
390
                public void discard(UnsavedDataPanel panel) {
391

    
392
                }
393
        }
394

    
395
        private class myList extends JPanel{
396

    
397
                /**
398
                 *
399
                 */
400
                private static final long serialVersionUID = 3179254463477354501L;
401

    
402
                private GridBagConstraints constraints;
403
                private GridBagConstraints constraintsLast;
404

    
405
                public myList(){
406
                        super();
407
                        initialize();
408
                }
409

    
410
                private void initialize(){
411
                        this.setLayout(new GridBagLayout());
412
                        this.constraints = new GridBagConstraints();
413
                        this.constraints.fill = GridBagConstraints.HORIZONTAL;
414
                        this.constraints.gridwidth = GridBagConstraints.REMAINDER;
415
                        this.constraints.weightx =0;
416
                        this.constraints.gridx =0;
417
                        this.constraints.gridy=0;
418
                        this.constraints.anchor = GridBagConstraints.WEST;
419
                        this.constraints.insets =new Insets(3,3,0,0);
420

    
421
                        this.constraintsLast = new GridBagConstraints();
422
                        this.constraintsLast.fill = GridBagConstraints.BOTH;
423
                        this.constraintsLast.gridheight = GridBagConstraints.REMAINDER;
424
                        this.constraintsLast.gridwidth = GridBagConstraints.REMAINDER;
425
                        this.constraintsLast.gridx =0;
426
                        this.constraintsLast.weightx =1;
427
                        this.constraintsLast.weighty =1;
428
                        this.constraintsLast.anchor = GridBagConstraints.FIRST_LINE_START;
429

    
430

    
431
                }
432

    
433
                public void addItems(IUnsavedData[] itemList){
434
                        int i;
435
                        this.removeAll();
436
                        if (itemList.length == 0){
437
                                return;
438
                        }
439

    
440
                        for (i=0;i< itemList.length;i++){
441
                                this.constraints.gridy = i;
442
                                this.add(newItem(itemList[i]), this.constraints);
443
                        }
444
                        this.constraintsLast.gridy=itemList.length;
445
                        this.add(new JLabel(),this.constraintsLast);
446

    
447
                }
448
                public int getItemsCount(){
449
                        return this.getComponentCount() -1;
450
                }
451

    
452
                public myUnsavedItem getItem(int i){
453
                        if (i>= this.getComponentCount()){
454
                                return null;
455
                        }
456
                        return (myUnsavedItem)this.getComponent(i);
457
                }
458

    
459
                private myUnsavedItem newItem(IUnsavedData itemData){
460
                        myUnsavedItem item;
461
                        item = new myUnsavedItem(itemData);
462
                        item.addMouseListener(new MouseListener(){
463
                                public void mouseClicked(MouseEvent e) {
464
                                        myUnsavedItem item = (myUnsavedItem)e.getComponent();
465
                                        getLblResourceDescription().setText(item.getDescription());
466
                                }
467
                                public void mouseEntered(MouseEvent e) {
468
                                }
469

    
470
                                public void mouseExited(MouseEvent e) {
471
                                }
472

    
473
                                public void mousePressed(MouseEvent e) {
474
                                }
475

    
476
                                public void mouseReleased(MouseEvent e) {
477
                                }
478

    
479
                        });
480
                        return item;
481
                }
482

    
483
        }
484

    
485
        private class myUnsavedItem extends JPanel{
486
                /**
487
                 *
488
                 */
489
                private static final long serialVersionUID = -6280057775368437349L;
490
                private JCheckBox chkChecked;
491
                private JLabel lblText;
492
                private IUnsavedData dataItem;
493

    
494
                public myUnsavedItem(IUnsavedData dataItem ){
495
                        super();
496
                        this.initialize();
497
                        this.setData(dataItem);
498
                }
499

    
500
                private void initialize(){
501
                        this.setOpaque(false);
502
                        BorderLayout layout = new BorderLayout();
503
                        layout.setHgap(5);
504
                        this.setLayout(layout);
505
                        this.add(this.getChkChecked(),BorderLayout.WEST);
506
                        this.add(this.getLblText(),BorderLayout.CENTER);
507
                }
508

    
509
                private JLabel getLblText() {
510
                        if (lblText == null){
511
                                lblText = new JLabel();
512
                                lblText.setName("lblText");
513
                        }
514
                        return lblText;
515
                }
516

    
517
                private JCheckBox getChkChecked() {
518
                        if (chkChecked == null){
519
                                chkChecked = new JCheckBox();
520
                                chkChecked.setName("chkChecked");
521
                                chkChecked.setSelected(true);
522
                                chkChecked.setOpaque(false);
523
                        }
524
                        return chkChecked;
525
                }
526

    
527
                public boolean isSelected(){
528
                        return getChkChecked().isSelected();
529
                }
530

    
531
                public void setSelected(boolean selected){
532
                        getChkChecked().setSelected(selected);
533
                }
534

    
535
                public void setData(IUnsavedData dataItem){
536
                        this.dataItem = dataItem;
537
                        JLabel lbl = getLblText();
538
                        if (dataItem.getIcon() != null){
539
                            ImageIcon ii = IconThemeHelper.getImageIcon(dataItem.getIcon());
540
                                lbl.setIcon(ii);
541
                        } else{
542
                                lbl.setIcon(getBlanckIcon());
543
                        }
544
                        lbl.setText(dataItem.getResourceName());
545
                        getChkChecked().setSelected(true);
546
                }
547

    
548
                public IUnsavedData getData(){
549
                        return this.dataItem;
550
                }
551

    
552
                public String getDescription(){
553
                        return this.dataItem.getDescription();
554
                }
555

    
556
                private ImageIcon getBlanckIcon(){
557
                        if (blanckIcon== null){
558
                                BufferedImage buff =new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
559
                                blanckIcon = new ImageIcon(buff);
560
                        }
561
                        return blanckIcon;
562
                }
563

    
564
        }
565

    
566
        public void actionPerformed(ActionEvent e) {
567
                if (e.getActionCommand() == "Cancel"){
568
                        fireDontExitEvent();
569
                        return;
570
                }
571
                if (e.getActionCommand() == "Accept"){
572
                        fireAcceptEvent();
573
                        return;
574
                }
575
                if (e.getActionCommand() == "Discard") {
576
                        fireDiscardEvent();
577
                        return;
578
                }
579
                if (e.getActionCommand() == "SelectAll"){
580
                        selectAll();
581
                        return;
582
                }
583
                if (e.getActionCommand() == "DeselectAll"){
584
                        deselectAll();
585
                        return;
586
                }
587
        }
588

    
589
        public void close(){
590
                PluginServices.getMDIManager().closeWindow(this);
591

    
592
        }
593

    
594
        private void fireAcceptEvent() {
595
                Iterator iter = this.listeners.iterator();
596
                while (iter.hasNext()){
597
                        ((UnsavedDataPanelListener)iter.next()).accept(this);
598
                }
599
        }
600

    
601
        private void fireDiscardEvent() {
602
                Iterator iter = this.listeners.iterator();
603
                while (iter.hasNext()){
604
                        ((UnsavedDataPanelListener)iter.next()).discard(this);
605
                }
606
        }
607

    
608
        private void fireDontExitEvent() {
609
                Iterator iter = this.listeners.iterator();
610
                while (iter.hasNext()){
611
                        ((UnsavedDataPanelListener)iter.next()).cancel(this);
612
                }
613
        }
614

    
615
        private void setSelectedsAll( boolean selected){
616
                int i;
617
                myList theList = getList();
618
                myUnsavedItem item;
619
                for (i=0;i < theList.getItemsCount();i++){
620
                        item = (myUnsavedItem)theList.getItem(i);
621
                        item.setSelected(selected);
622
                }
623
                theList.doLayout();
624
        }
625

    
626
        public void deselectAll() {
627
                this.setSelectedsAll(false);
628
        }
629

    
630
        public void selectAll() {
631
                this.setSelectedsAll(true);
632
        }
633

    
634

    
635
        /**
636
         * Set the resorces to ask for
637
         *
638
         * @param IUnsavedData[] resources pending to save
639
         */
640
        public void setUnsavedDataArray(IUnsavedData[] unsavedDatalist){
641
                this.unsavedDataList = unsavedDatalist;
642
                myList theList = getList();
643

    
644
                theList.addItems(unsavedDatalist);
645
                theList.validate();
646

    
647
        }
648

    
649

    
650
        private IUnsavedData[] getUnsavedData(boolean selected){
651
                int i;
652
                myList theList = getList();
653
                ArrayList aList = new ArrayList();
654
                myUnsavedItem item;
655
                IUnsavedData[] result;
656

    
657
                for (i=0;i < theList.getItemsCount();i++){
658
                        item = (myUnsavedItem)theList.getItem(i);
659
                        if (item.isSelected() == selected){
660
                                aList.add(item.getData());
661
                        }
662
                }
663
                result = new IUnsavedData[aList.size()];
664
                System.arraycopy(aList.toArray(), 0, result, 0, aList.size());
665
                return result;
666
        }
667

    
668

    
669
        /**
670
         * Returns the IUnsavedData selecteds for the user to save
671
         *
672
         * @return IUnsaveData[] to save
673
         */
674
        public IUnsavedData[] getSelectedsUnsavedData(){
675
                return this.getUnsavedData(true);
676

    
677
        }
678

    
679
        /**
680
         * Returns the IUnsavedData not selecteds for the user
681
         *
682
         * @return IUnsavedData[] to ignore
683
         */
684
        public IUnsavedData[] getUnselectedsUnsavedData(){
685
                return this.getUnsavedData(false);
686
        }
687

    
688
        public Object getWindowProfile() {
689
                return WindowInfo.DIALOG_PROFILE;
690
        }
691

    
692
}  //  @jve:decl-index=0:visual-constraint="10,10"
693