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 @ 42200

History | View | Annotate | Download (18.1 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.Arrays;
41
import java.util.Iterator;
42
import java.util.List;
43

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

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

    
58

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

    
70

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

    
85
        private ArrayList listeners = new ArrayList();
86
        private JLabel lblResourceDescription = null;
87
        private IUnsavedData[] unsavedDataList;
88
        private String windowTitle = PluginServices.getText(this, "save_resources");
89
        private String prompt = PluginServices.getText(this, "select_resources_to_save_before_exit");
90

    
91

    
92

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

    
102
        public UnsavedDataPanel(String prompt) {
103
        super();
104
        initialize();
105
        this.setUnsavedDataArray(new IUnsavedData[0]);
106
        this.prompt = prompt;
107
    }
108

    
109

    
110
        /**
111
         * This method initializes this
112
         *
113
         * @return void
114
         */
115
        private void initialize() {
116
                BorderLayout borderLayout = new BorderLayout();
117
                borderLayout.setHgap(5);
118
                borderLayout.setVgap(5);
119
                lblDescription = new JLabel();
120
                lblDescription.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
121
                lblDescription.setText(prompt);
122
                lblDescription.setPreferredSize(new Dimension(497, 30));
123
                lblDescription.setName("lblDescription");
124
                this.setLayout(borderLayout);
125
                this.setSize(396, 272);
126
                this.add(getPResources(), java.awt.BorderLayout.CENTER);
127
                this.add(getPButtons(), java.awt.BorderLayout.SOUTH);
128
                this.add(lblDescription, BorderLayout.NORTH);
129
        }
130

    
131
        /**
132
         * This method initializes pResources
133
         *
134
         * @return javax.swing.JPanel
135
         */
136
        private JPanel getPResources() {
137
                if (pResources == null) {
138
                        pResources = new JPanel();
139
                        pResources.setLayout(new BorderLayout());
140
                        pResources.add(getPScrollList(), BorderLayout.CENTER);
141
                        // pResources.add(getLblResourceDescription(), BorderLayout.SOUTH);
142
                }
143
                return pResources;
144
        }
145

    
146

    
147
        /**
148
         * This method initializes lblResourceDescription
149
         *
150
         * @return javax.swing.JLabel
151
         */
152
        private JLabel getLblResourceDescription() {
153
                if (lblResourceDescription == null) {
154
                        lblResourceDescription = new JLabel();
155
                        lblResourceDescription.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 0));
156
                        lblResourceDescription.setText("");
157
                        lblResourceDescription.setPreferredSize(new Dimension(38, 50));
158
                        lblResourceDescription.setName("lblResourceDescription");
159
                }
160
                return lblResourceDescription;
161
        }
162

    
163

    
164
        /**
165
         * This method initializes pButtons
166
         *
167
         * @return javax.swing.JPanel
168
         */
169
        private JPanel getPButtons() {
170
                if (pButtons == null) {
171
                        BorderLayout borderLayout2 = new BorderLayout();
172
                        borderLayout2.setHgap(5);
173
                        borderLayout2.setVgap(5);
174
                        pButtons = new JPanel();
175
                        pButtons.setLayout(borderLayout2);
176
                        pButtons.add(getPSelectionButtons(), BorderLayout.NORTH);
177
                        pButtons.add(getPActionButtons(), BorderLayout.SOUTH);
178
                }
179
                return pButtons;
180
        }
181

    
182
        /**
183
         * This method initializes pScrollList
184
         *
185
         * @return javax.swing.JScrollPane
186
         */
187
        private JScrollPane getPScrollList() {
188
                if (pScrollList == null) {
189
                        pScrollList = new JScrollPane();
190
                        pScrollList.setPreferredSize(new java.awt.Dimension(350,350));
191
                        pScrollList.setViewportView(getList());
192
                }
193
                return pScrollList;
194
        }
195

    
196
        /**
197
         * This method initializes list
198
         *
199
         * @return javax.swing.JList
200
         */
201
        private myList getList() {
202
                if (list == null) {
203
                        list = new myList();
204
                        list.setBackground(Color.white);
205
                }
206
                return list;
207
        }
208

    
209
        /**
210
         * This method initializes botSave
211
         *
212
         * @return javax.swing.JButton
213
         */
214
        private JButton getBotSave() {
215
                if (botSave == null) {
216
                        botSave = new JButton();
217
                        botSave.setName("botSave");
218
                        botSave.setToolTipText(PluginServices.getText(this, "save_selected_resources_and_exit"));
219
                        botSave.setText(PluginServices.getText(this, "ok"));
220
                        botSave.setActionCommand("Accept");
221
                        botSave.addActionListener(this);
222
                }
223
                return botSave;
224
        }
225

    
226
        /**
227
         * This method initializes botDiscard
228
         *
229
         * @return javax.swing.JButton
230
         */
231
        private JButton getBotDiscard() {
232
                if (botDiscard == null) {
233
                        botDiscard = new JButton();
234
                        botDiscard.setName("botDiscard");
235
                        botDiscard.setToolTipText(PluginServices.getText(this, "discard_changes_and_exit"));
236
                        botDiscard.setText(PluginServices.getText(this, "discard_changes"));
237
                        botDiscard.setActionCommand("Discard");
238
                        botDiscard.addActionListener(this);
239
                }
240
                return botDiscard;
241
        }
242

    
243
        /**
244
         * This method initializes botDontExit
245
         *
246
         * @return javax.swing.JButton
247
         */
248
        private JButton getBotDontExit() {
249
                if (botDontExit == null) {
250
                        botDontExit = new JButton();
251
                        botDontExit.setName("botDontExit");
252
                        botDontExit.setToolTipText(PluginServices.getText(this, "cancel_the_application_termination"));
253
                        botDontExit.setText(PluginServices.getText(this, "cancelar"));
254
                        botDontExit.setActionCommand("Cancel");
255
                        botDontExit.addActionListener(this);
256
                }
257
                return botDontExit;
258
        }
259

    
260
        /**
261
         * This method initializes pActionButtons
262
         *
263
         * @return javax.swing.JPanel
264
         */
265
        private JPanel getPActionButtons() {
266
                if (pActionButtons == null) {
267

    
268
                    GridLayout gl = new GridLayout(1, 3, 8, 0);
269
                    /*
270
                        FlowLayout flowLayout = new FlowLayout();
271
                        flowLayout.setAlignment(FlowLayout.RIGHT);
272
                        */
273
                        pActionButtons = new JPanel();
274
                        pActionButtons.setBorder(BorderFactory.createEmptyBorder(8,8,8,8));
275
                        pActionButtons.setLayout(gl);
276
                        pActionButtons.setName("pActionButtons");
277
                        pActionButtons.add(getBotSave());
278
                        pActionButtons.add(getBotDiscard());
279
                        pActionButtons.add(getBotDontExit());
280
                }
281
                return pActionButtons;
282
        }
283

    
284
        /**
285
         * This method initializes pSelectionButtons
286
         *
287
         * @return javax.swing.JPanel
288
         */
289
        private JPanel getPSelectionButtons() {
290
                if (pSelectionButtons == null) {
291
                    GridLayout gl = new GridLayout(2, 3, 8, 8);
292
                    /*
293
                        FlowLayout flowLayout1 = new FlowLayout();
294
                        flowLayout1.setAlignment(FlowLayout.LEFT);
295
                        */
296
                        pSelectionButtons = new JPanel();
297
                        pSelectionButtons.setBorder(BorderFactory.createEmptyBorder(5,8,8,8));
298
                        pSelectionButtons.setLayout(gl);
299
                        pSelectionButtons.setName("pSelectionButtons");
300
                        pSelectionButtons.add(getBotSelectAll());
301
                        pSelectionButtons.add(new JLabel());
302
                        pSelectionButtons.add(new JLabel());
303
                        // new row
304
                        pSelectionButtons.add(getBotDeselectAll());
305
                        pSelectionButtons.add(new JLabel());
306
            pSelectionButtons.add(new JLabel());
307

    
308
                }
309
                return pSelectionButtons;
310
        }
311

    
312
        /**
313
         * This method initializes botSelectAll
314
         *
315
         * @return javax.swing.JButton
316
         */
317
        private JButton getBotSelectAll() {
318
                if (botSelectAll == null) {
319
                        botSelectAll = new JButton();
320
                        botSelectAll.setText(PluginServices.getText(this, "select_all"));
321
                        botSelectAll.setName("botSelectAll");
322
                        botSelectAll.setToolTipText(PluginServices.getText(this, "select_all_resources"));
323
                        botSelectAll.setActionCommand("SelectAll");
324
                        botSelectAll.addActionListener(this);
325
                }
326
                return botSelectAll;
327
        }
328

    
329
        /**
330
         * This method initializes botDeselectAll
331
         *
332
         * @return javax.swing.JButton
333
         */
334
        private JButton getBotDeselectAll() {
335
                if (botDeselectAll == null) {
336
                        botDeselectAll = new JButton();
337
                        botDeselectAll.setName("botDeselectAll");
338
                        botDeselectAll.setToolTipText(PluginServices.getText(this, "deselect_all_resources"));
339
                        botDeselectAll.setText(PluginServices.getText(this, "deselect_all"));
340
                        botDeselectAll.setActionCommand("DeselectAll");
341
                        botDeselectAll.addActionListener(this);
342
                }
343
                return botDeselectAll;
344
        }
345

    
346
        public WindowInfo getWindowInfo() {
347
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG
348
                                | WindowInfo.RESIZABLE
349
                                | WindowInfo.MAXIMIZABLE);
350
                wi.setTitle(windowTitle);
351
                wi.setHeight(270);
352
                wi.setWidth(450);
353
                return wi;
354
        }
355

    
356
        public void setWindowTitle(String windowTitle) {
357
                this.windowTitle = windowTitle;
358
        }
359

    
360
        public void setCancelText(String text, String tooltipText) {
361
                getBotDontExit().setToolTipText(tooltipText);
362
                getBotDontExit().setText(text);
363
        }
364

    
365
        public void setDiscardText(String text, String tooltipText) {
366
                getBotDiscard().setToolTipText(tooltipText);
367
                getBotDiscard().setText(text);
368
        }
369

    
370
        public void setAcceptText(String text, String tooltipText) {
371
                getBotSave().setToolTipText(tooltipText);
372
                getBotSave().setText(text);
373
        }
374

    
375
        public void setHeaderText(String text) {
376
                lblDescription.setText(text);
377
        }
378

    
379
        /**
380
         * Register a listener to call
381
         *
382
         * @param a UnsavedDataPanelListener instance
383
         */
384
        public void addActionListener(UnsavedDataPanelListener listener){
385
                this.listeners.add(listener);
386
        }
387

    
388
        public void removeActionListener(UnsavedDataPanelListener listener){
389
                this.listeners.remove(listener);
390
        }
391

    
392
        public class UnsavedDataPanelListener {
393
                public void cancel(UnsavedDataPanel panel){
394

    
395
                }
396

    
397
                public void accept(UnsavedDataPanel panel){
398

    
399
                }
400

    
401
                public void discard(UnsavedDataPanel panel) {
402

    
403
                }
404
        }
405

    
406
        private class myList extends JPanel{
407

    
408
                /**
409
                 *
410
                 */
411
                private static final long serialVersionUID = 3179254463477354501L;
412

    
413
                private GridBagConstraints constraints;
414
                private GridBagConstraints constraintsLast;
415

    
416
                public myList(){
417
                        super();
418
                        initialize();
419
                }
420

    
421
                private void initialize(){
422
                        this.setLayout(new GridBagLayout());
423
                        this.constraints = new GridBagConstraints();
424
                        this.constraints.fill = GridBagConstraints.HORIZONTAL;
425
                        this.constraints.gridwidth = GridBagConstraints.REMAINDER;
426
                        this.constraints.weightx =0;
427
                        this.constraints.gridx =0;
428
                        this.constraints.gridy=0;
429
                        this.constraints.anchor = GridBagConstraints.WEST;
430
                        this.constraints.insets =new Insets(3,3,0,0);
431

    
432
                        this.constraintsLast = new GridBagConstraints();
433
                        this.constraintsLast.fill = GridBagConstraints.BOTH;
434
                        this.constraintsLast.gridheight = GridBagConstraints.REMAINDER;
435
                        this.constraintsLast.gridwidth = GridBagConstraints.REMAINDER;
436
                        this.constraintsLast.gridx =0;
437
                        this.constraintsLast.weightx =1;
438
                        this.constraintsLast.weighty =1;
439
                        this.constraintsLast.anchor = GridBagConstraints.FIRST_LINE_START;
440

    
441

    
442
                }
443

    
444
                public void addItems(IUnsavedData[] itemList){
445
                        int i;
446
                        this.removeAll();
447
                        if (itemList.length == 0){
448
                                return;
449
                        }
450

    
451
                        for (i=0;i< itemList.length;i++){
452
                                this.constraints.gridy = i;
453
                                this.add(newItem(itemList[i]), this.constraints);
454
                        }
455
                        this.constraintsLast.gridy=itemList.length;
456
                        this.add(new JLabel(),this.constraintsLast);
457

    
458
                }
459
                public int getItemsCount(){
460
                        return this.getComponentCount() -1;
461
                }
462

    
463
                public myUnsavedItem getItem(int i){
464
                        if (i>= this.getComponentCount()){
465
                                return null;
466
                        }
467
                        return (myUnsavedItem)this.getComponent(i);
468
                }
469

    
470
                private myUnsavedItem newItem(IUnsavedData itemData){
471
                        myUnsavedItem item;
472
                        item = new myUnsavedItem(itemData);
473
                        item.addMouseListener(new MouseListener(){
474
                                public void mouseClicked(MouseEvent e) {
475
                                        myUnsavedItem item = (myUnsavedItem)e.getComponent();
476
                                        getLblResourceDescription().setText(item.getDescription());
477
                                }
478
                                public void mouseEntered(MouseEvent e) {
479
                                }
480

    
481
                                public void mouseExited(MouseEvent e) {
482
                                }
483

    
484
                                public void mousePressed(MouseEvent e) {
485
                                }
486

    
487
                                public void mouseReleased(MouseEvent e) {
488
                                }
489

    
490
                        });
491
                        return item;
492
                }
493

    
494
        }
495

    
496
        private class myUnsavedItem extends JPanel{
497
                /**
498
                 *
499
                 */
500
                private static final long serialVersionUID = -6280057775368437349L;
501
                private JCheckBox chkChecked;
502
                private JLabel lblText;
503
                private IUnsavedData dataItem;
504

    
505
                public myUnsavedItem(IUnsavedData dataItem ){
506
                        super();
507
                        this.initialize();
508
                        this.setData(dataItem);
509
                }
510

    
511
                private void initialize(){
512
                        this.setOpaque(false);
513
                        BorderLayout layout = new BorderLayout();
514
                        layout.setHgap(5);
515
                        this.setLayout(layout);
516
                        this.add(this.getChkChecked(),BorderLayout.WEST);
517
                        this.add(this.getLblText(),BorderLayout.CENTER);
518
                }
519

    
520
                private JLabel getLblText() {
521
                        if (lblText == null){
522
                                lblText = new JLabel();
523
                                lblText.setName("lblText");
524
                        }
525
                        return lblText;
526
                }
527

    
528
                private JCheckBox getChkChecked() {
529
                        if (chkChecked == null){
530
                                chkChecked = new JCheckBox();
531
                                chkChecked.setName("chkChecked");
532
                                chkChecked.setSelected(true);
533
                                chkChecked.setOpaque(false);
534
                        }
535
                        return chkChecked;
536
                }
537

    
538
                public boolean isSelected(){
539
                        return getChkChecked().isSelected();
540
                }
541

    
542
                public void setSelected(boolean selected){
543
                        getChkChecked().setSelected(selected);
544
                }
545

    
546
                public void setData(IUnsavedData dataItem){
547
                        this.dataItem = dataItem;
548
                        JLabel lbl = getLblText();
549
                        if (dataItem.getIcon() != null){
550
                            ImageIcon ii = IconThemeHelper.getImageIcon(dataItem.getIcon());
551
                                lbl.setIcon(ii);
552
                        } else{
553
                                lbl.setIcon(getBlanckIcon());
554
                        }
555
                        lbl.setText(dataItem.getResourceName());
556
                        getChkChecked().setSelected(true);
557
                }
558

    
559
                public IUnsavedData getData(){
560
                        return this.dataItem;
561
                }
562

    
563
                public String getDescription(){
564
                        return this.dataItem.getDescription();
565
                }
566

    
567
                private ImageIcon getBlanckIcon(){
568
                        if (blanckIcon== null){
569
                                BufferedImage buff =new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
570
                                blanckIcon = new ImageIcon(buff);
571
                        }
572
                        return blanckIcon;
573
                }
574

    
575
        }
576

    
577
        public void actionPerformed(ActionEvent e) {
578
                if (e.getActionCommand() == "Cancel"){
579
                        fireDontExitEvent();
580
                        return;
581
                }
582
                if (e.getActionCommand() == "Accept"){
583
                        fireAcceptEvent();
584
                        return;
585
                }
586
                if (e.getActionCommand() == "Discard") {
587
                        fireDiscardEvent();
588
                        return;
589
                }
590
                if (e.getActionCommand() == "SelectAll"){
591
                        selectAll();
592
                        return;
593
                }
594
                if (e.getActionCommand() == "DeselectAll"){
595
                        deselectAll();
596
                        return;
597
                }
598
        }
599

    
600
        public void close(){
601
                PluginServices.getMDIManager().closeWindow(this);
602

    
603
        }
604

    
605
        private void fireAcceptEvent() {
606
                Iterator iter = this.listeners.iterator();
607
                while (iter.hasNext()){
608
                        ((UnsavedDataPanelListener)iter.next()).accept(this);
609
                }
610
        }
611

    
612
        private void fireDiscardEvent() {
613
                Iterator iter = this.listeners.iterator();
614
                while (iter.hasNext()){
615
                        ((UnsavedDataPanelListener)iter.next()).discard(this);
616
                }
617
        }
618

    
619
        private void fireDontExitEvent() {
620
                Iterator iter = this.listeners.iterator();
621
                while (iter.hasNext()){
622
                        ((UnsavedDataPanelListener)iter.next()).cancel(this);
623
                }
624
        }
625

    
626
        private void setSelectedsAll( boolean selected){
627
                int i;
628
                myList theList = getList();
629
                myUnsavedItem item;
630
                for (i=0;i < theList.getItemsCount();i++){
631
                        item = (myUnsavedItem)theList.getItem(i);
632
                        item.setSelected(selected);
633
                }
634
                theList.doLayout();
635
        }
636

    
637
        public void deselectAll() {
638
                this.setSelectedsAll(false);
639
        }
640

    
641
        public void selectAll() {
642
                this.setSelectedsAll(true);
643
        }
644

    
645

    
646
        /**
647
         * Set the resorces to ask for
648
         *
649
         * @param IUnsavedData[] resources pending to save
650
         */
651
        public void setUnsavedDataArray(IUnsavedData[] unsavedDatalist){
652
                this.unsavedDataList = unsavedDatalist;
653
                myList theList = getList();
654

    
655
                theList.addItems(unsavedDatalist);
656
                theList.validate();
657

    
658
        }
659

    
660
           /**
661
     * Set the resorces to ask for
662
     *
663
     * @param IUnsavedData[] resources pending to save
664
     */
665
    public void setUnsavedData(List<IUnsavedData> unsavedDatalist){
666
        setUnsavedDataArray(unsavedDatalist.toArray(new IUnsavedData[0]));
667
    }
668

    
669
    public List<IUnsavedData> getSelectedsUnsavedDataList(){
670
        return Arrays.asList(this.getSelectedsUnsavedData());
671
    }
672

    
673
        private IUnsavedData[] getUnsavedData(boolean selected){
674
                int i;
675
                myList theList = getList();
676
                ArrayList aList = new ArrayList();
677
                myUnsavedItem item;
678
                IUnsavedData[] result;
679

    
680
                for (i=0;i < theList.getItemsCount();i++){
681
                        item = (myUnsavedItem)theList.getItem(i);
682
                        if (item.isSelected() == selected){
683
                                aList.add(item.getData());
684
                        }
685
                }
686
                result = new IUnsavedData[aList.size()];
687
                System.arraycopy(aList.toArray(), 0, result, 0, aList.size());
688
                return result;
689
        }
690

    
691

    
692
        /**
693
         * Returns the IUnsavedData selecteds for the user to save
694
         *
695
         * @return IUnsaveData[] to save
696
         */
697
        public IUnsavedData[] getSelectedsUnsavedData(){
698
                return this.getUnsavedData(true);
699

    
700
        }
701

    
702
        /**
703
         * Returns the IUnsavedData not selecteds for the user
704
         *
705
         * @return IUnsavedData[] to ignore
706
         */
707
        public IUnsavedData[] getUnselectedsUnsavedData(){
708
                return this.getUnsavedData(false);
709
        }
710

    
711
        public Object getWindowProfile() {
712
                return WindowInfo.DIALOG_PROFILE;
713
        }
714

    
715
}  //  @jve:decl-index=0:visual-constraint="10,10"
716