Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2060 / frameworks / _fwAndami / src / org / gvsig / andami / ui / wizard / UnsavedDataPanel.java @ 39339

History | View | Annotate | Download (15.9 KB)

1
package org.gvsig.andami.ui.wizard;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.Color;
5
import java.awt.Dimension;
6
import java.awt.FlowLayout;
7
import java.awt.GridBagConstraints;
8
import java.awt.GridBagLayout;
9
import java.awt.Insets;
10
import java.awt.event.ActionEvent;
11
import java.awt.event.ActionListener;
12
import java.awt.event.MouseEvent;
13
import java.awt.event.MouseListener;
14
import java.awt.image.BufferedImage;
15
import java.util.ArrayList;
16
import java.util.Iterator;
17

    
18
import javax.swing.ImageIcon;
19
import javax.swing.JCheckBox;
20
import javax.swing.JLabel;
21
import javax.swing.JPanel;
22
import javax.swing.JScrollPane;
23

    
24
import org.gvsig.andami.IconThemeHelper;
25
import org.gvsig.andami.PluginServices;
26
import org.gvsig.andami.plugins.status.IUnsavedData;
27
import org.gvsig.andami.ui.mdiManager.IWindow;
28
import org.gvsig.andami.ui.mdiManager.WindowInfo;
29
import org.gvsig.gui.beans.swing.JButton;
30

    
31

    
32
/**
33
 * Ask for elements to save before application exit
34
 *
35
 *
36
 */
37
public class UnsavedDataPanel extends JPanel implements IWindow, ActionListener {
38
        /**
39
         *
40
         */
41
        private static final long serialVersionUID = -4745219917358767905L;
42

    
43

    
44
        private static ImageIcon blanckIcon = null;
45
        private JPanel pResources = null;
46
        private JPanel pButtons = null;
47
        private JScrollPane pScrollList = null;
48
        private myList list = null;
49
        private JLabel lblDescription = null;
50
        private JButton botSave = null;
51
        private JButton botDiscard = null;
52
        private JButton botDontExit = null;
53
        private JPanel pActionButtons = null;
54
        private JPanel pSelectionButtons = null;
55
        private JButton botSelectAll = null;
56
        private JButton botDeselectAll = null;
57

    
58
        private ArrayList listeners = new ArrayList();
59
        private JLabel lblResourceDescription = null;
60
        private IUnsavedData[] unsavedDataList;
61
        private String windowTitle = PluginServices.getText(this, "save_resources");
62

    
63

    
64

    
65
        /**
66
         * This is the default constructor
67
         */
68
        public UnsavedDataPanel(IUnsavedData[] unsavedDatalist) {
69
                super();
70
                initialize();
71
                this.setUnsavedDataArray(unsavedDatalist);
72
        }
73

    
74
        /**
75
         * This method initializes this
76
         *
77
         * @return void
78
         */
79
        private void initialize() {
80
                BorderLayout borderLayout = new BorderLayout();
81
                borderLayout.setHgap(5);
82
                borderLayout.setVgap(5);
83
                lblDescription = new JLabel();
84
                lblDescription.setText(PluginServices.getText(this, "select_resources_to_save_before_exit"));
85
                lblDescription.setPreferredSize(new Dimension(497, 40));
86
                lblDescription.setName("lblDescription");
87
                this.setLayout(borderLayout);
88
                this.setSize(396, 272);
89
                this.add(getPResources(), java.awt.BorderLayout.CENTER);
90
                this.add(getPButtons(), java.awt.BorderLayout.SOUTH);
91
                this.add(lblDescription, BorderLayout.NORTH);
92
        }
93

    
94
        /**
95
         * This method initializes pResources
96
         *
97
         * @return javax.swing.JPanel
98
         */
99
        private JPanel getPResources() {
100
                if (pResources == null) {
101
                        pResources = new JPanel();
102
                        pResources.setLayout(new BorderLayout());
103
                        pResources.add(getPScrollList(), BorderLayout.CENTER);
104
                        pResources.add(getLblResourceDescription(), BorderLayout.SOUTH);
105
                }
106
                return pResources;
107
        }
108

    
109

    
110
        /**
111
         * This method initializes lblResourceDescription
112
         *
113
         * @return javax.swing.JLabel
114
         */
115
        private JLabel getLblResourceDescription() {
116
                if (lblResourceDescription == null) {
117
                        lblResourceDescription = new JLabel();
118
                        lblResourceDescription.setText("");
119
                        lblResourceDescription.setPreferredSize(new Dimension(38, 50));
120
                        lblResourceDescription.setName("lblResourceDescription");
121
                }
122
                return lblResourceDescription;
123
        }
124

    
125

    
126
        /**
127
         * This method initializes pButtons
128
         *
129
         * @return javax.swing.JPanel
130
         */
131
        private JPanel getPButtons() {
132
                if (pButtons == null) {
133
                        BorderLayout borderLayout2 = new BorderLayout();
134
                        borderLayout2.setHgap(5);
135
                        borderLayout2.setVgap(5);
136
                        pButtons = new JPanel();
137
                        pButtons.setLayout(borderLayout2);
138
                        pButtons.add(getPSelectionButtons(), BorderLayout.NORTH);
139
                        pButtons.add(getPActionButtons(), BorderLayout.SOUTH);
140
                }
141
                return pButtons;
142
        }
143

    
144
        /**
145
         * This method initializes pScrollList
146
         *
147
         * @return javax.swing.JScrollPane
148
         */
149
        private JScrollPane getPScrollList() {
150
                if (pScrollList == null) {
151
                        pScrollList = new JScrollPane();
152
                        pScrollList.setPreferredSize(new java.awt.Dimension(350,350));
153
                        pScrollList.setViewportView(getList());
154
                }
155
                return pScrollList;
156
        }
157

    
158
        /**
159
         * This method initializes list
160
         *
161
         * @return javax.swing.JList
162
         */
163
        private myList getList() {
164
                if (list == null) {
165
                        list = new myList();
166
                        list.setBackground(Color.white);
167
                }
168
                return list;
169
        }
170

    
171
        /**
172
         * This method initializes botSave
173
         *
174
         * @return javax.swing.JButton
175
         */
176
        private JButton getBotSave() {
177
                if (botSave == null) {
178
                        botSave = new JButton();
179
                        botSave.setName("botSave");
180
                        botSave.setToolTipText(PluginServices.getText(this, "save_selected_resources_and_exit"));
181
                        botSave.setText(PluginServices.getText(this, "ok"));
182
                        botSave.setActionCommand("Accept");
183
                        botSave.addActionListener(this);
184
                }
185
                return botSave;
186
        }
187

    
188
        /**
189
         * This method initializes botDiscard
190
         *
191
         * @return javax.swing.JButton
192
         */
193
        private JButton getBotDiscard() {
194
                if (botDiscard == null) {
195
                        botDiscard = new JButton();
196
                        botDiscard.setName("botDiscard");
197
                        botDiscard.setToolTipText(PluginServices.getText(this, "discard_changes_and_exit"));
198
                        botDiscard.setText(PluginServices.getText(this, "discard_changes"));
199
                        botDiscard.setActionCommand("Discard");
200
                        botDiscard.addActionListener(this);
201
                }
202
                return botDiscard;
203
        }
204

    
205
        /**
206
         * This method initializes botDontExit
207
         *
208
         * @return javax.swing.JButton
209
         */
210
        private JButton getBotDontExit() {
211
                if (botDontExit == null) {
212
                        botDontExit = new JButton();
213
                        botDontExit.setName("botDontExit");
214
                        botDontExit.setToolTipText(PluginServices.getText(this, "cancel_the_application_termination"));
215
                        botDontExit.setText(PluginServices.getText(this, "cancelar"));
216
                        botDontExit.setActionCommand("Cancel");
217
                        botDontExit.addActionListener(this);
218
                }
219
                return botDontExit;
220
        }
221

    
222
        /**
223
         * This method initializes pActionButtons
224
         *
225
         * @return javax.swing.JPanel
226
         */
227
        private JPanel getPActionButtons() {
228
                if (pActionButtons == null) {
229
                        FlowLayout flowLayout = new FlowLayout();
230
                        flowLayout.setAlignment(FlowLayout.RIGHT);
231
                        pActionButtons = new JPanel();
232
                        pActionButtons.setLayout(flowLayout);
233
                        pActionButtons.setName("pActionButtons");
234
                        pActionButtons.add(getBotSave(), null);
235
                        pActionButtons.add(getBotDiscard(), null);
236
                        pActionButtons.add(getBotDontExit(), null);
237
                }
238
                return pActionButtons;
239
        }
240

    
241
        /**
242
         * This method initializes pSelectionButtons
243
         *
244
         * @return javax.swing.JPanel
245
         */
246
        private JPanel getPSelectionButtons() {
247
                if (pSelectionButtons == null) {
248
                        FlowLayout flowLayout1 = new FlowLayout();
249
                        flowLayout1.setAlignment(FlowLayout.LEFT);
250
                        pSelectionButtons = new JPanel();
251
                        pSelectionButtons.setLayout(flowLayout1);
252
                        pSelectionButtons.setName("pSelectionButtons");
253
                        pSelectionButtons.add(getBotSelectAll(), null);
254
                        pSelectionButtons.add(getBotDeselectAll(), null);
255
                }
256
                return pSelectionButtons;
257
        }
258

    
259
        /**
260
         * This method initializes botSelectAll
261
         *
262
         * @return javax.swing.JButton
263
         */
264
        private JButton getBotSelectAll() {
265
                if (botSelectAll == null) {
266
                        botSelectAll = new JButton();
267
                        botSelectAll.setText(PluginServices.getText(this, "select_all"));
268
                        botSelectAll.setName("botSelectAll");
269
                        botSelectAll.setToolTipText(PluginServices.getText(this, "select_all_resources"));
270
                        botSelectAll.setActionCommand("SelectAll");
271
                        botSelectAll.addActionListener(this);
272
                }
273
                return botSelectAll;
274
        }
275

    
276
        /**
277
         * This method initializes botDeselectAll
278
         *
279
         * @return javax.swing.JButton
280
         */
281
        private JButton getBotDeselectAll() {
282
                if (botDeselectAll == null) {
283
                        botDeselectAll = new JButton();
284
                        botDeselectAll.setName("botDeselectAll");
285
                        botDeselectAll.setToolTipText(PluginServices.getText(this, "deselect_all_resources"));
286
                        botDeselectAll.setText(PluginServices.getText(this, "deselect_all"));
287
                        botDeselectAll.setActionCommand("DeselectAll");
288
                        botDeselectAll.addActionListener(this);
289
                }
290
                return botDeselectAll;
291
        }
292

    
293
        public WindowInfo getWindowInfo() {
294
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG
295
                                | WindowInfo.RESIZABLE
296
                                | WindowInfo.MAXIMIZABLE);
297
                wi.setTitle(windowTitle);
298
                wi.setHeight(270);
299
                wi.setWidth(450);
300
                return wi;
301
        }
302

    
303
        public void setWindowTitle(String windowTitle) {
304
                this.windowTitle = windowTitle;
305
        }
306

    
307
        public void setCancelText(String text, String tooltipText) {
308
                getBotDontExit().setToolTipText(tooltipText);
309
                getBotDontExit().setText(text);
310
        }
311

    
312
        public void setDiscardText(String text, String tooltipText) {
313
                getBotDiscard().setToolTipText(tooltipText);
314
                getBotDiscard().setText(text);
315
        }
316

    
317
        public void setAcceptText(String text, String tooltipText) {
318
                getBotSave().setToolTipText(tooltipText);
319
                getBotSave().setText(text);
320
        }
321

    
322
        public void setHeaderText(String text) {
323
                lblDescription.setText(text);
324
        }
325

    
326
        /**
327
         * Register a listener to call
328
         *
329
         * @param a UnsavedDataPanelListener instance
330
         */
331
        public void addActionListener(UnsavedDataPanelListener listener){
332
                this.listeners.add(listener);
333
        }
334

    
335
        public void removeActionListener(UnsavedDataPanelListener listener){
336
                this.listeners.remove(listener);
337
        }
338

    
339
        public class UnsavedDataPanelListener {
340
                public void cancel(UnsavedDataPanel panel){
341

    
342
                }
343

    
344
                public void accept(UnsavedDataPanel panel){
345

    
346
                }
347

    
348
                public void discard(UnsavedDataPanel panel) {
349

    
350
                }
351
        }
352

    
353
        private class myList extends JPanel{
354

    
355
                /**
356
                 *
357
                 */
358
                private static final long serialVersionUID = 3179254463477354501L;
359

    
360
                private GridBagConstraints constraints;
361
                private GridBagConstraints constraintsLast;
362

    
363
                public myList(){
364
                        super();
365
                        initialize();
366
                }
367

    
368
                private void initialize(){
369
                        this.setLayout(new GridBagLayout());
370
                        this.constraints = new GridBagConstraints();
371
                        this.constraints.fill = GridBagConstraints.HORIZONTAL;
372
                        this.constraints.gridwidth = GridBagConstraints.REMAINDER;
373
                        this.constraints.weightx =0;
374
                        this.constraints.gridx =0;
375
                        this.constraints.gridy=0;
376
                        this.constraints.anchor = GridBagConstraints.WEST;
377
                        this.constraints.insets =new Insets(3,3,0,0);
378

    
379
                        this.constraintsLast = new GridBagConstraints();
380
                        this.constraintsLast.fill = GridBagConstraints.BOTH;
381
                        this.constraintsLast.gridheight = GridBagConstraints.REMAINDER;
382
                        this.constraintsLast.gridwidth = GridBagConstraints.REMAINDER;
383
                        this.constraintsLast.gridx =0;
384
                        this.constraintsLast.weightx =1;
385
                        this.constraintsLast.weighty =1;
386
                        this.constraintsLast.anchor = GridBagConstraints.FIRST_LINE_START;
387

    
388

    
389
                }
390

    
391
                public void addItems(IUnsavedData[] itemList){
392
                        int i;
393
                        this.removeAll();
394
                        if (itemList.length == 0){
395
                                return;
396
                        }
397

    
398
                        for (i=0;i< itemList.length;i++){
399
                                this.constraints.gridy = i;
400
                                this.add(newItem(itemList[i]), this.constraints);
401
                        }
402
                        this.constraintsLast.gridy=itemList.length;
403
                        this.add(new JLabel(),this.constraintsLast);
404

    
405
                }
406
                public int getItemsCount(){
407
                        return this.getComponentCount() -1;
408
                }
409

    
410
                public myUnsavedItem getItem(int i){
411
                        if (i>= this.getComponentCount()){
412
                                return null;
413
                        }
414
                        return (myUnsavedItem)this.getComponent(i);
415
                }
416

    
417
                private myUnsavedItem newItem(IUnsavedData itemData){
418
                        myUnsavedItem item;
419
                        item = new myUnsavedItem(itemData);
420
                        item.addMouseListener(new MouseListener(){
421
                                public void mouseClicked(MouseEvent e) {
422
                                        myUnsavedItem item = (myUnsavedItem)e.getComponent();
423
                                        getLblResourceDescription().setText(item.getDescription());
424
                                }
425
                                public void mouseEntered(MouseEvent e) {
426
                                }
427

    
428
                                public void mouseExited(MouseEvent e) {
429
                                }
430

    
431
                                public void mousePressed(MouseEvent e) {
432
                                }
433

    
434
                                public void mouseReleased(MouseEvent e) {
435
                                }
436

    
437
                        });
438
                        return item;
439
                }
440

    
441
        }
442

    
443
        private class myUnsavedItem extends JPanel{
444
                /**
445
                 *
446
                 */
447
                private static final long serialVersionUID = -6280057775368437349L;
448
                private JCheckBox chkChecked;
449
                private JLabel lblText;
450
                private IUnsavedData dataItem;
451

    
452
                public myUnsavedItem(IUnsavedData dataItem ){
453
                        super();
454
                        this.initialize();
455
                        this.setData(dataItem);
456
                }
457

    
458
                private void initialize(){
459
                        this.setOpaque(false);
460
                        BorderLayout layout = new BorderLayout();
461
                        layout.setHgap(5);
462
                        this.setLayout(layout);
463
                        this.add(this.getChkChecked(),BorderLayout.WEST);
464
                        this.add(this.getLblText(),BorderLayout.CENTER);
465
                }
466

    
467
                private JLabel getLblText() {
468
                        if (lblText == null){
469
                                lblText = new JLabel();
470
                                lblText.setName("lblText");
471
                        }
472
                        return lblText;
473
                }
474

    
475
                private JCheckBox getChkChecked() {
476
                        if (chkChecked == null){
477
                                chkChecked = new JCheckBox();
478
                                chkChecked.setName("chkChecked");
479
                                chkChecked.setSelected(true);
480
                                chkChecked.setOpaque(false);
481
                        }
482
                        return chkChecked;
483
                }
484

    
485
                public boolean isSelected(){
486
                        return getChkChecked().isSelected();
487
                }
488

    
489
                public void setSelected(boolean selected){
490
                        getChkChecked().setSelected(selected);
491
                }
492

    
493
                public void setData(IUnsavedData dataItem){
494
                        this.dataItem = dataItem;
495
                        JLabel lbl = getLblText();
496
                        if (dataItem.getIcon() != null){
497
                            ImageIcon ii = IconThemeHelper.getImageIcon(dataItem.getIcon());
498
                                lbl.setIcon(ii);
499
                        } else{
500
                                lbl.setIcon(getBlanckIcon());
501
                        }
502
                        lbl.setText(dataItem.getResourceName());
503
                        getChkChecked().setSelected(true);
504
                }
505

    
506
                public IUnsavedData getData(){
507
                        return this.dataItem;
508
                }
509

    
510
                public String getDescription(){
511
                        return this.dataItem.getDescription();
512
                }
513

    
514
                private ImageIcon getBlanckIcon(){
515
                        if (blanckIcon== null){
516
                                BufferedImage buff =new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
517
                                blanckIcon = new ImageIcon(buff);
518
                        }
519
                        return blanckIcon;
520
                }
521

    
522
        }
523

    
524
        public void actionPerformed(ActionEvent e) {
525
                if (e.getActionCommand() == "Cancel"){
526
                        fireDontExitEvent();
527
                        return;
528
                }
529
                if (e.getActionCommand() == "Accept"){
530
                        fireAcceptEvent();
531
                        return;
532
                }
533
                if (e.getActionCommand() == "Discard") {
534
                        fireDiscardEvent();
535
                        return;
536
                }
537
                if (e.getActionCommand() == "SelectAll"){
538
                        selectAll();
539
                        return;
540
                }
541
                if (e.getActionCommand() == "DeselectAll"){
542
                        deselectAll();
543
                        return;
544
                }
545
        }
546

    
547
        public void close(){
548
                PluginServices.getMDIManager().closeWindow(this);
549

    
550
        }
551

    
552
        private void fireAcceptEvent() {
553
                Iterator iter = this.listeners.iterator();
554
                while (iter.hasNext()){
555
                        ((UnsavedDataPanelListener)iter.next()).accept(this);
556
                }
557
        }
558

    
559
        private void fireDiscardEvent() {
560
                Iterator iter = this.listeners.iterator();
561
                while (iter.hasNext()){
562
                        ((UnsavedDataPanelListener)iter.next()).discard(this);
563
                }
564
        }
565

    
566
        private void fireDontExitEvent() {
567
                Iterator iter = this.listeners.iterator();
568
                while (iter.hasNext()){
569
                        ((UnsavedDataPanelListener)iter.next()).cancel(this);
570
                }
571
        }
572

    
573
        private void setSelectedsAll( boolean selected){
574
                int i;
575
                myList theList = getList();
576
                myUnsavedItem item;
577
                for (i=0;i < theList.getItemsCount();i++){
578
                        item = (myUnsavedItem)theList.getItem(i);
579
                        item.setSelected(selected);
580
                }
581
                theList.doLayout();
582
        }
583

    
584
        public void deselectAll() {
585
                this.setSelectedsAll(false);
586
        }
587

    
588
        public void selectAll() {
589
                this.setSelectedsAll(true);
590
        }
591

    
592

    
593
        /**
594
         * Set the resorces to ask for
595
         *
596
         * @param IUnsavedData[] resources pending to save
597
         */
598
        public void setUnsavedDataArray(IUnsavedData[] unsavedDatalist){
599
                this.unsavedDataList = unsavedDatalist;
600
                myList theList = getList();
601

    
602
                theList.addItems(unsavedDatalist);
603
                theList.validate();
604

    
605
        }
606

    
607

    
608
        private IUnsavedData[] getUnsavedData(boolean selected){
609
                int i;
610
                myList theList = getList();
611
                ArrayList aList = new ArrayList();
612
                myUnsavedItem item;
613
                IUnsavedData[] result;
614

    
615
                for (i=0;i < theList.getItemsCount();i++){
616
                        item = (myUnsavedItem)theList.getItem(i);
617
                        if (item.isSelected() == selected){
618
                                aList.add(item.getData());
619
                        }
620
                }
621
                result = new IUnsavedData[aList.size()];
622
                System.arraycopy(aList.toArray(), 0, result, 0, aList.size());
623
                return result;
624
        }
625

    
626

    
627
        /**
628
         * Returns the IUnsavedData selecteds for the user to save
629
         *
630
         * @return IUnsaveData[] to save
631
         */
632
        public IUnsavedData[] getSelectedsUnsavedData(){
633
                return this.getUnsavedData(true);
634

    
635
        }
636

    
637
        /**
638
         * Returns the IUnsavedData not selecteds for the user
639
         *
640
         * @return IUnsavedData[] to ignore
641
         */
642
        public IUnsavedData[] getUnselectedsUnsavedData(){
643
                return this.getUnsavedData(false);
644
        }
645

    
646
        public Object getWindowProfile() {
647
                return WindowInfo.DIALOG_PROFILE;
648
        }
649

    
650
}  //  @jve:decl-index=0:visual-constraint="10,10"
651