Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.hyperlink.app / org.gvsig.hyperlink.app.extension / src / main / java / org / gvsig / hyperlink / app / extension / config / gui / ConfigTab.java @ 33402

History | View | Annotate | Download (18.5 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
package org.gvsig.hyperlink.app.extension.config.gui;
24

    
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.awt.event.ItemEvent;
31
import java.awt.event.ItemListener;
32
import java.util.ArrayList;
33
import java.util.Iterator;
34

    
35
import javax.swing.BorderFactory;
36
import javax.swing.JButton;
37
import javax.swing.JCheckBox;
38
import javax.swing.JPanel;
39
import javax.swing.JScrollPane;
40
import javax.swing.border.EtchedBorder;
41
import javax.swing.border.TitledBorder;
42

    
43
import org.gvsig.app.project.documents.view.legend.gui.AbstractThemeManagerPage;
44
import org.gvsig.fmap.mapcontext.layers.FLayer;
45
import org.gvsig.hyperlink.app.extension.AbstractActionManager;
46
import org.gvsig.hyperlink.app.extension.HyperlinkExtension;
47
import org.gvsig.hyperlink.app.extension.ILinkActionManager;
48
import org.gvsig.hyperlink.app.extension.config.LayerLinkConfig;
49
import org.gvsig.hyperlink.app.extension.config.LinkConfig;
50
import org.gvsig.hyperlink.app.extension.layers.ILinkLayerManager;
51
import org.gvsig.hyperlink.app.extension.layers.IncompatibleLayerException;
52
import org.gvsig.hyperlink.app.extension.layers.ManagerRegistry;
53
import org.gvsig.tools.ToolsLocator;
54
import org.gvsig.tools.extensionpoint.ExtensionPoint;
55
import org.gvsig.tools.extensionpoint.ExtensionPoint.Extension;
56
//import org.gvsig.hyperlink.app.extension.IncompatibleLayerException;
57
//import org.gvsig.hyperlink.app.extension.ManagerRegistry;
58

    
59
import org.gvsig.andami.PluginServices;
60
import org.gvsig.andami.ui.mdiManager.IWindow;
61
import org.gvsig.andami.ui.mdiManager.WindowInfo;
62

    
63
public class ConfigTab extends AbstractThemeManagerPage implements IWindow,
64
    ActionListener, ItemListener {
65

    
66
    FLayer layer;
67
    ArrayList rows = new ArrayList();
68
    JPanel rowList = null;
69
    GridBagLayout listLayout = null;
70
    String[] actionCodes = null;
71
    String[] actionNames = null;
72
    WindowInfo _windowInfo = null;
73
    JButton jbt_accept = null, jbt_cancel = null;
74
    JPanel emptyRow;
75
    JCheckBox jcb_enabled = null;
76
    JPanel borderPanel = null;
77
    JButton jbt_addAction = null, jbt_removeAction = null;
78

    
79
    public ConfigTab() {
80
        super();
81
        initialize();
82
    }
83

    
84
    private void initialize() {
85
        this.setLayout(new GridBagLayout());
86

    
87
        Insets insets = new Insets(8, 8, 8, 8);
88
        GridBagConstraints constraints = new GridBagConstraints();
89
        constraints.gridx = 0;
90
        constraints.gridy = 0;
91
        constraints.weightx = 1.0;
92
        constraints.fill = GridBagConstraints.NONE;
93
        constraints.insets = insets;
94
        constraints.anchor = GridBagConstraints.NORTHWEST;
95
        this.add(getEnabledCheckBox(), constraints);
96

    
97
        borderPanel = new JPanel(new GridBagLayout());
98
        borderPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
99
            PluginServices.getText(this, "Actions"),
100
            TitledBorder.DEFAULT_JUSTIFICATION,
101
            TitledBorder.DEFAULT_POSITION,
102
            null,
103
            null));
104

    
105
        insets = new Insets(8, 8, 8, 8);
106
        constraints = new GridBagConstraints();
107
        constraints.gridx = 0;
108
        constraints.gridy = 0;
109
        constraints.weightx = 1.0;
110
        constraints.fill = GridBagConstraints.NONE;
111
        constraints.insets = insets;
112
        constraints.anchor = GridBagConstraints.NORTHWEST;
113
        borderPanel.add(getAddRemoveActionsPanel(), constraints);
114

    
115
        listLayout = new GridBagLayout();
116
        rowList = new JPanel(listLayout);
117

    
118
        constraints.gridx = 0;
119
        constraints.gridy = 0;
120
        constraints.weightx = 1.0;
121
        constraints.weighty = 1.0;
122
        constraints.fill = GridBagConstraints.NONE;
123
        constraints.insets = insets;
124
        constraints.anchor = GridBagConstraints.SOUTH;
125
        emptyRow = new JPanel();
126
        rowList.add(emptyRow, constraints);
127

    
128
        JScrollPane scrolledList = new JScrollPane();
129
        scrolledList.setBorder(null);
130
        scrolledList.setViewportView(rowList);
131

    
132
        constraints.gridx = 0;
133
        constraints.gridy = 1;
134
        constraints.weightx = 1.0;
135
        constraints.weighty = 1.0;
136
        constraints.fill = GridBagConstraints.BOTH;
137
        constraints.insets = insets;
138
        constraints.anchor = GridBagConstraints.CENTER;
139
        borderPanel.add(scrolledList, constraints);
140

    
141
        constraints.gridx = 0;
142
        constraints.gridy = 1;
143
        constraints.weightx = 1.0;
144
        constraints.weighty = 1.0;
145
        constraints.fill = GridBagConstraints.BOTH;
146
        constraints.insets = insets;
147
        constraints.anchor = GridBagConstraints.NORTHWEST;
148
        this.add(borderPanel, constraints);
149

    
150
        // initAcceptCancelButtons(); uncomment this if you want to use this
151
        // window outside the ThemeManager
152
    }
153

    
154
    private void initAcceptCancelButtons() {
155
        JPanel acceptCancelButtons = new JPanel(new GridBagLayout());
156
        Insets insets = new Insets(14, 4, 8, 8);
157
        GridBagConstraints constraints = new GridBagConstraints();
158
        constraints.gridx = 0;
159
        constraints.gridy = 0;
160
        constraints.weightx = 1.0;
161
        constraints.fill = GridBagConstraints.NONE;
162
        constraints.insets = insets;
163
        constraints.anchor = GridBagConstraints.EAST;
164
        acceptCancelButtons.add(getAcceptButton(), constraints);
165

    
166
        constraints.gridx = 1;
167
        constraints.gridy = 0;
168
        constraints.weightx = 0.0;
169
        constraints.fill = GridBagConstraints.NONE;
170
        constraints.insets = insets;
171
        constraints.anchor = GridBagConstraints.EAST;
172
        acceptCancelButtons.add(getCancelButton(), constraints);
173

    
174
        constraints.gridx = 0;
175
        constraints.gridy = 2;
176
        constraints.weightx = 0.0;
177
        constraints.fill = GridBagConstraints.NONE;
178
        constraints.insets = insets;
179
        constraints.anchor = GridBagConstraints.SOUTHEAST;
180
        this.add(acceptCancelButtons, constraints);
181
    }
182

    
183
    private String[] getActionNames() {
184
        if (actionNames == null) {
185
            // ExtensionPoint actions = (ExtensionPoint)
186
            // ExtensionPointsSingleton.getInstance().get("HyperLinkAction");
187
            ExtensionPoint actions =
188
                ToolsLocator.getExtensionPointManager()
189
                    .get(HyperlinkExtension.ACTIONSEXTENSIONPOINT);
190

    
191
            // String[] actionArray = new String[actions.size()];
192
            String[] actionArray = new String[actions.getCount()];
193
            // Iterator actionsIter = actions.iterator();
194
            // int i = 0;
195
            // while (actionsIter.hasNext()) {
196
            // Extension action = (Extension)actionsIter.next();
197
            // actionArray[i++] = action.getName();
198
            // }
199
            // actionNames = actionArray;
200
            String[] codes = getActionCodes();
201
            for (int i = 0; i < codes.length; i++) {
202
                // actionArray[i] = actions.get(codes[i]).getName();
203
                try {
204
                    AbstractActionManager aam =
205
                        (AbstractActionManager) actions.get(codes[i])
206
                            .getExtension()
207
                            .newInstance();
208
                    actionArray[i] = aam.getName();
209
                } catch (InstantiationException e) {
210
                    // TODO Auto-generated catch block
211
                    e.printStackTrace();
212
                } catch (IllegalAccessException e) {
213
                    // TODO Auto-generated catch block
214
                    e.printStackTrace();
215
                }
216
                // actionArray[i] = actions.get(codes[i]).getName();
217
            }
218
            actionNames = actionArray;
219
        }
220
        return actionNames;
221
    }
222

    
223
    private String[] getActionCodes() {
224
        if (actionCodes == null) {
225
            // ExtensionPoint actions = (ExtensionPoint)
226
            // ExtensionPointsSingleton.getInstance().get("HyperLinkAction");
227
            ExtensionPoint actions =
228
                ToolsLocator.getExtensionPointManager()
229
                    .get(HyperlinkExtension.ACTIONSEXTENSIONPOINT);
230
            // String[] actionArray = new String[actions.size()];
231
            String[] actionArray = new String[actions.getCount()];
232
            // Iterator actionsIter = actions.values().iterator();
233
            actionArray = (String[]) actions.getNames().toArray(actionArray);
234
            // Iterator actionsIter = actions.iterator();
235
            // int i = 0;
236
            // while (actionsIter.hasNext()) {
237
            // ILinkActionManager action = (ILinkActionManager)
238
            // actionsIter.next();
239
            // actionArray[i++] = action.getActionCode();
240
            // }
241
            actionCodes = actionArray;
242
        }
243
        return actionCodes;
244
    }
245

    
246
    private String[] getCandidateFields() {
247
        HyperlinkExtension ext =
248
            (HyperlinkExtension) PluginServices.getExtension(HyperlinkExtension.class);
249
        ManagerRegistry registry = ext.getLayerManager();
250
        try {
251
            ILinkLayerManager manager;
252
            manager = registry.get(layer);
253
            manager.setLayer(layer);
254
            return manager.getFieldCandidates();
255
        } catch (InstantiationException e) {
256
            PluginServices.getLogger()
257
                .warn("Hyperlink: error getting candidate fields", e);
258
        } catch (IllegalAccessException e) {
259
            PluginServices.getLogger()
260
                .warn("Hyperlink: error getting candidate fields", e);
261
        } catch (ClassNotFoundException e) {
262
            PluginServices.getLogger()
263
                .warn("Hyperlink: error getting candidate fields", e);
264
        } catch (IncompatibleLayerException e) {
265
            PluginServices.getLogger()
266
                .warn("Hyperlink: error getting candidate fields", e);
267
        }
268
        return new String[0];
269
    }
270

    
271
    public void acceptAction() {
272
        applyAction();
273
    }
274

    
275
    public void applyAction() {
276
        LayerLinkConfig config = new LayerLinkConfig();
277
        for (int i = 0; i < rows.size(); i++) {
278
            LinkRow row = (LinkRow) rows.get(i);
279
            config.addLink(getActionCodes()[row.getSelectedAction()],
280
                row.getSelectedField(),
281
                row.getExtension());
282
        }
283
        config.setEnabled(getEnabledCheckBox().isSelected());
284
        layer.setProperty(HyperlinkExtension.LAYERPROPERTYNAME, config);
285
    }
286

    
287
    public void cancelAction() {
288
    }
289

    
290
    public String getName() {
291
        return PluginServices.getText(this, "Hyperlink");
292
    }
293

    
294
    public void setModel(FLayer layer) {
295
        this.layer = layer;
296
        synchronized (rows) {
297
            HyperlinkExtension ext =
298
                (HyperlinkExtension) PluginServices.getExtension(HyperlinkExtension.class);
299
            LayerLinkConfig layerConfig = ext.loadLegacyConfig(layer);
300
            for (int i = rows.size() - 1; i >= 0; i--) { // clean rows
301
                LinkRow row = (LinkRow) rows.remove(i);
302
                rowList.remove(row);
303
            }
304

    
305
            if (layerConfig != null) {
306
                for (int i = 0; i < layerConfig.linkCount(); i++) {
307
                    LinkRow row = addRow();
308
                    LinkConfig config = layerConfig.getLink(i);
309
                    String selectedAction = config.getActionCode();
310
                    String[] actions = getActionCodes();
311
                    for (int j = 0; j < actions.length; j++) {
312
                        if (actions[j].equals(selectedAction)) {
313
                            row.setSelectedAction(j);
314
                        }
315
                    }
316
                    row.setSelectedField(config.getFieldName());
317
                    row.setExtension(config.getExtension());
318
                }
319
                if (layerConfig.linkCount() < 1) {
320
                    addRow(); // just one row by default
321
                }
322
                setLinkEnabled(layerConfig.isEnabled());
323
            } else {
324
                addRow(); // just one row by default
325
                setLinkEnabled(false);
326
            }
327
        }
328
    }
329

    
330
    protected JPanel getAddRemoveActionsPanel() {
331
        JPanel container = new JPanel();
332
        container.add(getAddActionButton());
333
        container.add(getRemoveActionButton());
334
        return container;
335
    }
336

    
337
    protected LinkRow addRow() {
338
        LinkRow row = new LinkRow();
339
        row.setFields(getCandidateFields());
340
        row.setActions(getActionNames());
341
        synchronized (rows) {
342
            rows.add(row);
343

    
344
            GridBagConstraints constraints = new GridBagConstraints();
345
            constraints.gridx = 0;
346
            constraints.gridy = rows.size() - 1;
347
            constraints.weightx = 1.0;
348
            constraints.weighty = 0.0;
349
            constraints.fill = GridBagConstraints.HORIZONTAL;
350
            constraints.insets = new Insets(2, 4, 2, 4);
351
            constraints.anchor = GridBagConstraints.NORTH;
352
            rowList.add(row, constraints);
353

    
354
            constraints = new GridBagConstraints();
355
            constraints.gridx = 0;
356
            constraints.gridy = rows.size();
357
            constraints.weightx = 1.0;
358
            constraints.weighty = 1.0;
359
            constraints.fill = GridBagConstraints.NONE;
360
            constraints.anchor = GridBagConstraints.CENTER;
361
            listLayout.setConstraints(emptyRow, constraints);
362
        }
363
        validate();
364
        return row;
365
    }
366

    
367
    protected boolean removeBottomRow() {
368
        LinkRow row;
369
        synchronized (rows) {
370
            if (rows.size() <= 1)
371
                return false;
372
            row = (LinkRow) rows.get(rows.size() - 1);
373
            rows.remove(rows.size() - 1);
374
        }
375
        rowList.remove(row);
376
        GridBagConstraints constraints = new GridBagConstraints();
377
        constraints.gridx = 0;
378
        constraints.gridy = rows.size();
379
        constraints.weightx = 1.0;
380
        constraints.weighty = 1.0;
381
        constraints.fill = GridBagConstraints.NONE;
382
        constraints.anchor = GridBagConstraints.CENTER;
383
        listLayout.setConstraints(emptyRow, constraints);
384

    
385
        validate();
386
        repaint();
387
        return true;
388
    }
389

    
390
    protected JButton getAddActionButton() {
391
        if (jbt_addAction == null) {
392
            jbt_addAction =
393
                new JButton(PluginServices.getText(this, "Add_action"));
394
            jbt_addAction.setSize(150, 70);
395
            jbt_addAction.setActionCommand("addButton");
396
            jbt_addAction.addActionListener(new ActionListener() {
397

    
398
                public void actionPerformed(ActionEvent e) {
399
                    if (e.getActionCommand().equals("addButton")) {
400
                        addRow();
401
                    }
402
                }
403
            });
404
        }
405
        return jbt_addAction;
406
    }
407

    
408
    protected JButton getRemoveActionButton() {
409
        if (jbt_removeAction == null) {
410
            jbt_removeAction =
411
                new JButton(PluginServices.getText(this, "Remove_action"));
412
            jbt_removeAction.setActionCommand("removeButton");
413
            jbt_removeAction.addActionListener(new ActionListener() {
414

    
415
                public void actionPerformed(ActionEvent e) {
416
                    if (e.getActionCommand().equals("removeButton")) {
417
                        removeBottomRow();
418
                    }
419
                }
420
            });
421
            return jbt_removeAction;
422
        }
423
        return jbt_removeAction;
424
    }
425

    
426
    public WindowInfo getWindowInfo() {
427
        if (_windowInfo == null) {
428
            _windowInfo =
429
                new WindowInfo(WindowInfo.ICONIFIABLE | WindowInfo.MAXIMIZABLE
430
                    | WindowInfo.RESIZABLE);
431
            _windowInfo.setWidth(680);
432
            _windowInfo.setHeight(500);
433
            _windowInfo.setTitle(PluginServices.getText(this,
434
                "Hyperlink_Settings"));
435
        }
436
        return _windowInfo;
437
    }
438

    
439
    protected JButton getAcceptButton() {
440
        if (jbt_accept == null) {
441
            jbt_accept =
442
                new org.gvsig.gui.beans.swing.JButton(PluginServices.getText(this,
443
                    "Aceptar"));
444
            jbt_accept.addActionListener(this);
445
        }
446
        return jbt_accept;
447
    }
448

    
449
    protected JButton getCancelButton() {
450
        if (jbt_cancel == null) {
451
            jbt_cancel =
452
                new org.gvsig.gui.beans.swing.JButton(PluginServices.getText(this,
453
                    "Cancel"));
454
            jbt_cancel.addActionListener(this);
455
        }
456
        return jbt_cancel;
457
    }
458

    
459
    protected JCheckBox getEnabledCheckBox() {
460
        if (jcb_enabled == null) {
461
            jcb_enabled =
462
                new JCheckBox(PluginServices.getText(this, "Enable_hyperlink"),
463
                    true);
464
            jcb_enabled.addItemListener(this);
465
        }
466
        return jcb_enabled;
467
    }
468

    
469
    public void actionPerformed(ActionEvent e) {
470
        if (e.getSource() == getAcceptButton()) {
471
            acceptAction();
472
            PluginServices.getMDIManager().closeWindow(this);
473

    
474
        } else
475
            if (e.getSource() == getCancelButton()) {
476
                cancelAction();
477
                PluginServices.getMDIManager().closeWindow(this);
478
            }
479
    }
480

    
481
    protected void setListEnabled(boolean enabled) {
482
        getAddActionButton().setEnabled(enabled);
483
        getRemoveActionButton().setEnabled(enabled);
484
        synchronized (rows) {
485
            for (int i = 0; i < rows.size(); i++) {
486
                LinkRow row = (LinkRow) rows.get(i);
487
                row.setEnabled(enabled);
488
            }
489
        }
490
    }
491

    
492
    protected void setLinkEnabled(boolean enabled) {
493
        getEnabledCheckBox().setSelected(enabled);
494
        setListEnabled(enabled);
495
    }
496

    
497
    public void itemStateChanged(ItemEvent e) {
498
        if (e.getSource() == getEnabledCheckBox()) {
499
            if (e.getStateChange() == ItemEvent.DESELECTED) {
500
                setListEnabled(false);
501
            } else {
502
                setListEnabled(true);
503
            }
504
        }
505
    }
506

    
507
    public Object getWindowProfile() {
508
        return WindowInfo.DIALOG_PROFILE;
509
    }
510
}