Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / org.gvsig.installer / org.gvsig.installer.swing / org.gvsig.installer.swing.impl / src / main / java / org / gvsig / installer / swing / impl / execution / panel / PackagesTablePanel.java @ 38423

History | View | Annotate | Download (11 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
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2010 {Prodevelop}   {Task}
26
 */
27

    
28
package org.gvsig.installer.swing.impl.execution.panel;
29

    
30
import java.awt.Color;
31
import java.awt.GridBagConstraints;
32
import java.awt.GridBagLayout;
33
import java.awt.Insets;
34
import java.awt.event.MouseEvent;
35
import java.awt.event.MouseListener;
36
import java.net.URL;
37
import java.util.List;
38

    
39
import javax.swing.JEditorPane;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.JTable;
43
import javax.swing.event.ListSelectionEvent;
44
import javax.swing.event.ListSelectionListener;
45
import javax.swing.table.TableColumnModel;
46

    
47
import org.gvsig.installer.lib.api.PackageInfo;
48
import org.gvsig.installer.swing.api.SwingInstallerLocator;
49
import org.gvsig.installer.swing.api.SwingInstallerManager;
50
import org.gvsig.installer.swing.impl.execution.panel.filters.NameDescriptionOrCodeFilter;
51
import org.gvsig.installer.swing.impl.execution.panel.filters.PackageFilter;
52
import org.gvsig.installer.swing.impl.execution.panel.model.PackagesTableModel;
53
import org.gvsig.installer.swing.impl.execution.panel.model.PackagesTableModel.PackageOfficialRecommended;
54
import org.gvsig.installer.swing.impl.execution.panel.model.PackagesTableModel.PackageOsAndArchitecture;
55
import org.gvsig.installer.swing.impl.execution.panel.model.PackagesTableModel.PackageStatus;
56
import org.gvsig.installer.swing.impl.execution.panel.renderers.AbstractTablePackageInfoCellRenderer;
57
import org.gvsig.installer.swing.impl.execution.panel.renderers.InstallStatusCellEditor;
58
import org.gvsig.installer.swing.impl.execution.panel.renderers.InstallStatusCellRenderer;
59
import org.gvsig.installer.swing.impl.execution.panel.renderers.InstalledPackageCellRenderer;
60
import org.gvsig.installer.swing.impl.execution.panel.renderers.OfficialRecommendedCellRenderer;
61
import org.gvsig.installer.swing.impl.execution.panel.renderers.OsAndArchitectureCellRenderer;
62

    
63
/**
64
 * @author gvSIG Team
65
 * @version $Id$
66
 */
67
public class PackagesTablePanel extends JPanel implements ListSelectionListener {
68

    
69
        private static final long serialVersionUID = 8156088357208685689L;
70
        protected SwingInstallerManager swingInstallerManager = null;
71

    
72
        private JScrollPane descriptionScrollPane;
73
        private JEditorPane descriptionTextEditor;
74
        private JScrollPane pluginsScrollPane;
75
        private JTable pluginsTable;
76
        private PackagePropertiesFilterPanel filterPanel;
77
        private FastFilterButtonsPanel fastFilterPanel;
78
        private SelectPackagesPanel selectPackagesPanel;
79

    
80
        public PackagesTablePanel(SelectPackagesPanel selectPackagesPanel) {
81
                super();
82
                swingInstallerManager = SwingInstallerLocator
83
                                .getSwingInstallerManager();
84
                this.selectPackagesPanel = selectPackagesPanel;
85
                initComponents();
86
                pluginsTable.getSelectionModel().addListSelectionListener(this);
87
                pluginsTable.setDefaultRenderer(PackageOfficialRecommended.class,
88
                                new OfficialRecommendedCellRenderer());
89
                pluginsTable.setDefaultRenderer(PackageStatus.class,
90
                                new InstallStatusCellRenderer());
91
                pluginsTable.setDefaultEditor(PackageStatus.class,
92
                                new InstallStatusCellEditor(this.selectPackagesPanel));
93
                AbstractTablePackageInfoCellRenderer ipcr = new InstalledPackageCellRenderer();
94
                pluginsTable.setDefaultRenderer(PackageOsAndArchitecture.class,
95
                                new OsAndArchitectureCellRenderer());
96
                pluginsTable.setDefaultRenderer(String.class, ipcr);
97
                pluginsTable.setDefaultRenderer(Number.class, ipcr);
98
                pluginsTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
99
        }
100

    
101
        private void initComponents() {
102
                java.awt.GridBagConstraints gridBagConstraints;
103

    
104
                pluginsTable = new JTable();
105

    
106
                MouseListener mouseListener = new MyMouseListener();
107
                pluginsTable.addMouseListener(mouseListener);
108

    
109
                pluginsScrollPane = new JScrollPane(pluginsTable);
110

    
111
                descriptionTextEditor = new JEditorPane();
112
                descriptionTextEditor.setBackground(Color.WHITE);
113
                descriptionTextEditor.setEditable(false);
114
                descriptionTextEditor.setContentType("text/html");
115

    
116
                descriptionScrollPane = new JScrollPane(descriptionTextEditor);
117

    
118
                filterPanel = new PackagePropertiesFilterPanel(this);
119
                filterPanel.setVisible(true);
120

    
121
                fastFilterPanel = new FastFilterButtonsPanel(this);
122

    
123
                setLayout(new GridBagLayout());
124

    
125
                // left panel filter
126
                gridBagConstraints = new GridBagConstraints();
127
                gridBagConstraints.fill = GridBagConstraints.BOTH;
128
                gridBagConstraints.gridx = 0;
129
                gridBagConstraints.gridy = 1;
130
                gridBagConstraints.weightx = 0.13;
131
                gridBagConstraints.weighty = 1;
132
                gridBagConstraints.gridheight = 2;
133
                gridBagConstraints.insets = new Insets(0, 2, 2, 2);
134
                add(filterPanel, gridBagConstraints);
135

    
136
                // fast filters buttons panel
137
                gridBagConstraints = new GridBagConstraints();
138
                gridBagConstraints.fill = GridBagConstraints.BOTH;
139
                gridBagConstraints.gridx = 1;
140
                gridBagConstraints.gridy = 0;
141
                gridBagConstraints.weightx = 0.75;
142
                gridBagConstraints.weighty = 0;
143
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
144
                add(fastFilterPanel, gridBagConstraints);
145

    
146
                // plugins scroll panel
147
                gridBagConstraints = new GridBagConstraints();
148
                gridBagConstraints.fill = GridBagConstraints.BOTH;
149
                gridBagConstraints.gridx = 1;
150
                gridBagConstraints.gridy = 1;
151
                gridBagConstraints.weightx = 0.75;
152
                gridBagConstraints.weighty = 0.7;
153
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
154
                add(pluginsScrollPane, gridBagConstraints);
155

    
156
                // description panel
157
                gridBagConstraints = new GridBagConstraints();
158
                gridBagConstraints.gridx = 1;
159
                gridBagConstraints.gridy = 2;
160
                gridBagConstraints.fill = GridBagConstraints.BOTH;
161
                gridBagConstraints.weightx = 0.75;
162
                gridBagConstraints.weighty = 0.3;
163
                gridBagConstraints.insets = new Insets(2, 2, 2, 2);
164
                add(descriptionScrollPane, gridBagConstraints);
165
                
166
                // filterPanel.setInitialFilter();
167
        }
168

    
169
        public void resetFilter() {
170
                filterPanel.resetPanel();
171
                valueChanged();
172
        }
173
        
174
        public void selectPackages() {
175
                PackagesTableModel pluginsTableModel = (PackagesTableModel) pluginsTable
176
                                .getModel();
177
                pluginsTableModel.selectDefaultPackages();
178
        }
179

    
180
        public void setTableModel(PackagesTableModel pluginsTableModel) {
181
                pluginsTable.setModel(pluginsTableModel);
182
                pluginsTableModel.fireTableDataChanged();
183
                TableColumnModel tableColumnModel = pluginsTable.getColumnModel();
184
                tableColumnModel.getColumn(0).setPreferredWidth(20);
185
                tableColumnModel.getColumn(1).setPreferredWidth(20);
186
                tableColumnModel.getColumn(2).setPreferredWidth(20);
187
                tableColumnModel.getColumn(3).setPreferredWidth(225);
188
                tableColumnModel.getColumn(4).setPreferredWidth(130);
189
                tableColumnModel.getColumn(5).setPreferredWidth(53);
190

    
191
        }
192

    
193
        public List<PackageInfo> getPackagesToInstall() {
194
                return ((PackagesTableModel) pluginsTable.getModel())
195
                                .getPackagesToInstall();
196
        }
197

    
198
        public boolean isPackageSelected() {
199
                return ((PackagesTableModel) pluginsTable.getModel())
200
                                .hasAnyPackageSelected();
201
        }
202

    
203
        private class HTMLCoder {
204
                private StringBuffer buffer = new StringBuffer();
205
        
206
                public String toString() {
207
                        return buffer.toString();
208
                }
209
                
210
                public void addTitle(String title) {
211
                        if( title == null ) {
212
                                return;
213
                        }
214
                        buffer.append("<h1>");
215
                        buffer.append(title);
216
                        buffer.append("</h1>");
217
                }
218
                
219
                public void addParagraph(Object text) {
220
                        if( text == null ) {
221
                                return;
222
                        }
223
                        String t = text.toString().replaceAll("\\n", "<br>\n");
224
                        buffer.append("<p>");
225
                        buffer.append(t);
226
                        buffer.append("</p>");
227
                }
228
                
229
                public void addBeginList() {
230
                        buffer.append("<ul>");
231
                }
232
                
233
                public void addEndList() {
234
                        buffer.append("</ul>");
235
                }
236
                
237
                public void addListItem(String label, Object value) {
238
                        if( value == null ) {
239
                                return;
240
                        }
241
                        buffer.append("<li>");
242
                        buffer.append(label);
243
                        buffer.append(": <i>");
244
                        buffer.append(value.toString());
245
                        buffer.append("</i></li>");
246
                }
247
                
248
                public void addListItem(String label, URL value) {
249
                        if( value == null ) {
250
                                return;
251
                        }
252
                        buffer.append("<li>");
253
                        buffer.append(label);
254
                        buffer.append(": <a href=\"");
255
                        buffer.append(value.toString());
256
                        buffer.append("\">");
257
                        buffer.append(value.toString());
258
                        buffer.append("</a></li>");
259
                }
260
                
261
        }
262
        public void valueChanged(ListSelectionEvent e) {
263
                valueChanged();
264
        }
265

    
266
        public void valueChanged() {
267
                int row = pluginsTable.getSelectedRow();
268
                if (row < 0) {
269
                        descriptionTextEditor.setText("<p></p>");
270
                        return;
271
                }
272

    
273
                PackageInfo pkginfo = ((PackagesTableModel) pluginsTable.getModel())
274
                                .getPackageInfoAt(row);
275

    
276
                HTMLCoder html = new HTMLCoder();
277
                html.addTitle(pkginfo.getName());
278
                html.addParagraph(pkginfo.getDescription());
279
                html.addBeginList();
280
                html.addListItem("code", pkginfo.getCode());
281
                html.addListItem("Organization", pkginfo.getOwner());
282
                html.addListItem("Organization URL", pkginfo.getOwnerURL());
283
                html.addListItem("Sources", pkginfo.getSourcesURL());
284
                html.addListItem("Dependencies", pkginfo.getDependencies());
285
                html.addListItem("Categories", pkginfo.getCategoriesAsString());
286
                html.addListItem("Official", pkginfo.isOfficial());
287
                html.addListItem("Signed", pkginfo.isSigned());
288
                html.addEndList();
289

    
290
                descriptionTextEditor.setText(html.toString());
291

    
292
                // get the view area to the top-left corner
293
                descriptionTextEditor.setCaretPosition(0);
294

    
295
        }
296

    
297
        public void setFilter(PackageFilter filter) {
298
                PackagesTableModel pluginsTableModel = (PackagesTableModel) pluginsTable
299
                                .getModel();
300
                pluginsTableModel.setFilter(filter);
301
                selectPackagesPanel.updatePanel();
302
        }
303

    
304
        public void setFilter(NameDescriptionOrCodeFilter filter) {
305
                PackagesTableModel pluginsTableModel = (PackagesTableModel) pluginsTable
306
                                .getModel();
307
                pluginsTableModel.setFilter(filter);
308
                selectPackagesPanel.updatePanel();
309
        }
310

    
311
        private class MyMouseListener implements MouseListener {
312

    
313
                public void mouseClicked(MouseEvent e) {
314
                        selectPackagesPanel.checkIfPluginSelected();
315
                }
316

    
317
                public void mouseEntered(MouseEvent e) {
318
                }
319

    
320
                public void mouseExited(MouseEvent e) {
321
                }
322

    
323
                public void mousePressed(MouseEvent e) {
324
                }
325

    
326
                public void mouseReleased(MouseEvent e) {
327
                }
328
        }
329

    
330
        public SelectPackagesPanel getSelectPackagesPanel() {
331
                return this.selectPackagesPanel;
332
        }
333

    
334
        public void clearAllPanels() {
335
                filterPanel.resetPanel();
336
                descriptionTextEditor.setText("");
337
                fastFilterPanel.resetPanel();
338
        }
339

    
340
        public void resetPanel() {
341
                filterPanel.resetPanel();
342
                descriptionTextEditor.setText("");
343
        }
344

    
345
    /**
346
     * 
347
     */
348
    public void setInitialFilter() {
349
        filterPanel.setInitialFilter();
350
    }
351

    
352
}