Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / frameworks / _fwAndami / src / com / iver / andami / ui / wizard / UnsavedDataPanel.java @ 12621

History | View | Annotate | Download (14.4 KB)

1
package com.iver.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.gui.beans.swing.JButton;
25

    
26
import com.iver.andami.PluginServices;
27
import com.iver.andami.plugins.status.IUnsavedData;
28
import com.iver.andami.ui.mdiManager.IWindow;
29
import com.iver.andami.ui.mdiManager.WindowInfo;
30

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

    
42

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

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

    
61

    
62

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

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

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

    
107

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

    
123

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

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

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

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

    
186
        /**
187
         * This method initializes botDontExit
188
         *
189
         * @return javax.swing.JButton
190
         */
191
        private JButton getBotDontExit() {
192
                if (botDontExit == null) {
193
                        botDontExit = new JButton();
194
                        botDontExit.setName("botDontExit");
195
                        botDontExit.setToolTipText(PluginServices.getText(this, "cancel_the_application_exit"));
196
                        botDontExit.setText(PluginServices.getText(this, "dont_exit"));
197
                        botDontExit.setActionCommand("DontExit");
198
                        botDontExit.addActionListener(this);
199
                }
200
                return botDontExit;
201
        }
202

    
203
        /**
204
         * This method initializes pActionButtons
205
         *
206
         * @return javax.swing.JPanel
207
         */
208
        private JPanel getPActionButtons() {
209
                if (pActionButtons == null) {
210
                        FlowLayout flowLayout = new FlowLayout();
211
                        flowLayout.setAlignment(FlowLayout.RIGHT);
212
                        pActionButtons = new JPanel();
213
                        pActionButtons.setLayout(flowLayout);
214
                        pActionButtons.setName("pActionButtons");
215
                        pActionButtons.add(getBotSave(), null);
216
                        pActionButtons.add(getBotDontExit(), null);
217
                }
218
                return pActionButtons;
219
        }
220

    
221
        /**
222
         * This method initializes pSelectionButtons
223
         *
224
         * @return javax.swing.JPanel
225
         */
226
        private JPanel getPSelectionButtons() {
227
                if (pSelectionButtons == null) {
228
                        FlowLayout flowLayout1 = new FlowLayout();
229
                        flowLayout1.setAlignment(FlowLayout.LEFT);
230
                        pSelectionButtons = new JPanel();
231
                        pSelectionButtons.setLayout(flowLayout1);
232
                        pSelectionButtons.setName("pSelectionButtons");
233
                        pSelectionButtons.add(getBotSelectAll(), null);
234
                        pSelectionButtons.add(getBotDeselectAll(), null);
235
                }
236
                return pSelectionButtons;
237
        }
238

    
239
        /**
240
         * This method initializes botSelectAll
241
         *
242
         * @return javax.swing.JButton
243
         */
244
        private JButton getBotSelectAll() {
245
                if (botSelectAll == null) {
246
                        botSelectAll = new JButton();
247
                        botSelectAll.setText(PluginServices.getText(this, "select_all"));
248
                        botSelectAll.setName("botSelectAll");
249
                        botSelectAll.setToolTipText(PluginServices.getText(this, "select_all_resources"));
250
                        botSelectAll.setActionCommand("SelectAll");
251
                        botSelectAll.addActionListener(this);
252
                }
253
                return botSelectAll;
254
        }
255

    
256
        /**
257
         * This method initializes botDeselectAll
258
         *
259
         * @return javax.swing.JButton
260
         */
261
        private JButton getBotDeselectAll() {
262
                if (botDeselectAll == null) {
263
                        botDeselectAll = new JButton();
264
                        botDeselectAll.setName("botDeselectAll");
265
                        botDeselectAll.setToolTipText(PluginServices.getText(this, "deselect_all_resources"));
266
                        botDeselectAll.setText(PluginServices.getText(this, "deselect_all"));
267
                        botDeselectAll.setActionCommand("DeselectAll");
268
                        botDeselectAll.addActionListener(this);
269
                }
270
                return botDeselectAll;
271
        }
272

    
273
        public WindowInfo getWindowInfo() {
274
                WindowInfo wi = new WindowInfo(WindowInfo.MODALDIALOG
275
                                | WindowInfo.RESIZABLE
276
                                | WindowInfo.MAXIMIZABLE);
277
                wi.setTitle(windowTitle);
278
                wi.setHeight(500);
279
                wi.setWidth(400);
280
                return wi;
281
        }
282

    
283
        public void setWindowTitle(String windowTitle) {
284
                this.windowTitle = windowTitle;
285
        }
286
        
287
        /**
288
         * Register a listener to call
289
         *
290
         * @param a UnsavedDataPanelListener instance
291
         */
292
        public void addActionListener(UnsavedDataPanelListener listener){
293
                this.listeners.add(listener);
294
        }
295

    
296
        public void removeActionListener(UnsavedDataPanelListener listener){
297
                this.listeners.remove(listener);
298
        }
299

    
300
        public class UnsavedDataPanelListener {
301
                public void dontExit(UnsavedDataPanel panel){
302

    
303
                }
304

    
305
                public void saveData(UnsavedDataPanel panel){
306

    
307
                }
308
        }
309

    
310
        private class myList extends JPanel{
311

    
312
                /**
313
                 *
314
                 */
315
                private static final long serialVersionUID = 3179254463477354501L;
316

    
317
                private GridBagConstraints constraints;
318
                private GridBagConstraints constraintsLast;
319

    
320
                public myList(){
321
                        super();
322
                        initialize();
323
                }
324

    
325
                private void initialize(){
326
                        this.setLayout(new GridBagLayout());
327
                        this.constraints = new GridBagConstraints();
328
                        this.constraints.fill = GridBagConstraints.HORIZONTAL;
329
                        this.constraints.gridwidth = GridBagConstraints.REMAINDER;
330
                        this.constraints.weightx =0;
331
                        this.constraints.gridx =0;
332
                        this.constraints.gridy=0;
333
                        this.constraints.anchor = GridBagConstraints.WEST;
334
                        this.constraints.insets =new Insets(3,3,0,0);
335

    
336
                        this.constraintsLast = new GridBagConstraints();
337
                        this.constraintsLast.fill = GridBagConstraints.BOTH;
338
                        this.constraintsLast.gridheight = GridBagConstraints.REMAINDER;
339
                        this.constraintsLast.gridwidth = GridBagConstraints.REMAINDER;
340
                        this.constraintsLast.gridx =0;
341
                        this.constraintsLast.weightx =1;
342
                        this.constraintsLast.weighty =1;
343
                        this.constraintsLast.anchor = GridBagConstraints.FIRST_LINE_START;
344

    
345

    
346
                }
347

    
348
                public void addItems(IUnsavedData[] itemList){
349
                        int i;
350
                        this.removeAll();
351
                        if (itemList.length == 0){
352
                                return;
353
                        }
354

    
355
                        for (i=0;i< itemList.length;i++){
356
                                this.constraints.gridy = i;
357
                                this.add(newItem(itemList[i]), this.constraints);
358
                        }
359
                        this.constraintsLast.gridy=itemList.length;
360
                        this.add(new JLabel(),this.constraintsLast);
361

    
362
                }
363
                public int getItemsCount(){
364
                        return this.getComponentCount() -1;
365
                }
366

    
367
                public myUnsavedItem getItem(int i){
368
                        if (i>= this.getComponentCount()){
369
                                return null;
370
                        }
371
                        return (myUnsavedItem)this.getComponent(i);
372
                }
373

    
374
                private myUnsavedItem newItem(IUnsavedData itemData){
375
                        myUnsavedItem item;
376
                        item = new myUnsavedItem(itemData);
377
                        item.addMouseListener(new MouseListener(){
378
                                public void mouseClicked(MouseEvent e) {
379
                                        myUnsavedItem item = (myUnsavedItem)e.getComponent();
380
                                        getLblResourceDescription().setText(item.getDescription());
381
                                }
382
                                public void mouseEntered(MouseEvent e) {
383
                                }
384

    
385
                                public void mouseExited(MouseEvent e) {
386
                                }
387

    
388
                                public void mousePressed(MouseEvent e) {
389
                                }
390

    
391
                                public void mouseReleased(MouseEvent e) {
392
                                }
393

    
394
                        });
395
                        return item;
396
                }
397

    
398
        }
399

    
400
        private class myUnsavedItem extends JPanel{
401
                /**
402
                 *
403
                 */
404
                private static final long serialVersionUID = -6280057775368437349L;
405
                private JCheckBox chkChecked;
406
                private JLabel lblText;
407
                private IUnsavedData dataItem;
408

    
409
                public myUnsavedItem(IUnsavedData dataItem ){
410
                        super();
411
                        this.initialize();
412
                        this.setData(dataItem);
413
                }
414

    
415
                private void initialize(){
416
                        this.setOpaque(false);
417
                        BorderLayout layout = new BorderLayout();
418
                        layout.setHgap(5);
419
                        this.setLayout(layout);
420
                        this.add(this.getChkChecked(),BorderLayout.WEST);
421
                        this.add(this.getLblText(),BorderLayout.CENTER);
422
                }
423

    
424
                private JLabel getLblText() {
425
                        if (lblText == null){
426
                                lblText = new JLabel();
427
                                lblText.setName("lblText");
428
                        }
429
                        return lblText;
430
                }
431

    
432
                private JCheckBox getChkChecked() {
433
                        if (chkChecked == null){
434
                                chkChecked = new JCheckBox();
435
                                chkChecked.setName("chkChecked");
436
                                chkChecked.setSelected(true);
437
                                chkChecked.setOpaque(false);
438
                        }
439
                        return chkChecked;
440
                }
441

    
442
                public boolean isSelected(){
443
                        return getChkChecked().isSelected();
444
                }
445

    
446
                public void setSelected(boolean selected){
447
                        getChkChecked().setSelected(selected);
448
                }
449

    
450
                public void setData(IUnsavedData dataItem){
451
                        this.dataItem = dataItem;
452
                        JLabel lbl = getLblText();
453
                        if (dataItem.getIcon() != null){
454
                                lbl.setIcon(dataItem.getIcon());
455
                        } else{
456
                                lbl.setIcon(getBlanckIcon());
457
                        }
458
                        lbl.setText(dataItem.getResourceName());
459
                        getChkChecked().setSelected(true);
460
                }
461

    
462
                public IUnsavedData getData(){
463
                        return this.dataItem;
464
                }
465

    
466
                public String getDescription(){
467
                        return this.dataItem.getDescription();
468
                }
469

    
470
                private ImageIcon getBlanckIcon(){
471
                        if (blanckIcon== null){
472
                                BufferedImage buff =new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
473
                                blanckIcon = new ImageIcon(buff);
474
                        }
475
                        return blanckIcon;
476
                }
477

    
478
        }
479

    
480
        public void actionPerformed(ActionEvent e) {
481
                if (e.getActionCommand() == "DontExit"){
482
                        fireDontExitEvent();
483
                        this.close();
484
                        return;
485
                }
486
                if (e.getActionCommand() == "Save"){
487
                        fireSaveEvent();
488
                        this.close();
489
                        return;
490
                }
491
                if (e.getActionCommand() == "SelectAll"){
492
                        selectAll();
493
                        return;
494
                }
495
                if (e.getActionCommand() == "DeselectAll"){
496
                        deselectAll();
497
                        return;
498
                }
499
        }
500

    
501
        public void close(){
502
                PluginServices.getMDIManager().closeWindow(this);
503

    
504
        }
505

    
506
        private void fireSaveEvent() {
507
                Iterator iter = this.listeners.iterator();
508
                while (iter.hasNext()){
509
                        ((UnsavedDataPanelListener)iter.next()).saveData(this);
510
                }
511

    
512
        }
513

    
514
        private void fireDontExitEvent() {
515
                Iterator iter = this.listeners.iterator();
516
                while (iter.hasNext()){
517
                        ((UnsavedDataPanelListener)iter.next()).dontExit(this);
518
                }
519
        }
520

    
521
        private void setSelectedsAll( boolean selected){
522
                int i;
523
                myList theList = getList();
524
                myUnsavedItem item;
525
                for (i=0;i < theList.getItemsCount();i++){
526
                        item = (myUnsavedItem)theList.getItem(i);
527
                        item.setSelected(selected);
528
                }
529
                theList.doLayout();
530
        }
531

    
532
        public void deselectAll() {
533
                this.setSelectedsAll(false);
534
        }
535

    
536
        public void selectAll() {
537
                this.setSelectedsAll(true);
538
        }
539

    
540

    
541
        /**
542
         * Set the resorces to ask for
543
         *
544
         * @param IUnsavedData[] resources pending to save
545
         */
546
        public void setUnsavedDataArray(IUnsavedData[] unsavedDatalist){
547
                this.unsavedDataList = unsavedDatalist;
548
                myList theList = getList();
549

    
550
                theList.addItems(unsavedDatalist);
551

    
552
        }
553

    
554

    
555
        private IUnsavedData[] getUnsavedData(boolean selected){
556
                int i;
557
                myList theList = getList();
558
                ArrayList aList = new ArrayList();
559
                myUnsavedItem item;
560
                IUnsavedData[] result;
561

    
562
                for (i=0;i < theList.getItemsCount();i++){
563
                        item = (myUnsavedItem)theList.getItem(i);
564
                        if (item.isSelected() == selected){
565
                                aList.add(item.getData());
566
                        }
567
                }
568
                result = new IUnsavedData[aList.size()];
569
                System.arraycopy(aList.toArray(), 0, result, 0, aList.size());
570
                return result;
571
        }
572

    
573

    
574
        /**
575
         * Returns the IUnsavedData selecteds for the user to save
576
         *
577
         * @return IUnsaveData[] to save
578
         */
579
        public IUnsavedData[] getSelectedsUnsavedData(){
580
                return this.getUnsavedData(true);
581

    
582
        }
583

    
584
        /**
585
         * Returns the IUnsavedData not selecteds for the user
586
         *
587
         * @return IUnsavedData[] to ignore
588
         */
589
        public IUnsavedData[] getUnselectedsUnsavedData(){
590
                return this.getUnsavedData(false);
591
        }
592

    
593
}  //  @jve:decl-index=0:visual-constraint="10,10"
594