Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / CheckSOAndArquitectureExtension.java @ 40558

History | View | Annotate | Download (8.36 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.app.extension;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.ComponentOrientation;
28
import java.awt.Container;
29
import java.awt.Dimension;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32
import java.io.File;
33
import java.net.URL;
34
import java.util.HashSet;
35
import java.util.Iterator;
36
import java.util.Set;
37

    
38
import javax.swing.Box;
39
import javax.swing.JButton;
40
import javax.swing.JPanel;
41
import javax.swing.JScrollPane;
42
import javax.swing.JTextPane;
43

    
44
import org.apache.commons.io.FileUtils;
45
import org.gvsig.andami.PluginsLocator;
46
import org.gvsig.andami.PluginsManager;
47
import org.gvsig.andami.plugins.Extension;
48
import org.gvsig.app.ApplicationLocator;
49
import org.gvsig.app.ApplicationManager;
50
import org.gvsig.installer.lib.api.InstallerLocator;
51
import org.gvsig.installer.lib.api.InstallerManager;
52
import org.gvsig.installer.lib.api.PackageInfo;
53
import org.slf4j.Logger;
54
import org.slf4j.LoggerFactory;
55

    
56
import com.jgoodies.forms.layout.CellConstraints;
57
import com.jgoodies.forms.layout.FormLayout;
58

    
59
public class CheckSOAndArquitectureExtension extends Extension {
60

    
61
        private static Logger logger = LoggerFactory.getLogger(CheckSOAndArquitectureExtension.class);
62
        public void initialize() {
63
                // Do nothing
64
        }
65

    
66
        public void execute(String actionCommand) {
67
                // Do nothing
68

    
69
        }
70

    
71
        public boolean isEnabled() {
72
                return false;
73
        }
74

    
75
        public boolean isVisible() {
76
                return false;
77
        }
78

    
79
        public void postInitialize() {
80
                ApplicationManager application = ApplicationLocator.getManager();
81
                InstallerManager installmgr = InstallerLocator.getInstallerManager();
82
                PluginsManager pluginmgr = PluginsLocator.getManager();
83

    
84
                Set<PackageInfo>  mismatchs = new HashSet<PackageInfo>();
85
                try {
86
                        PackageInfo[] pkgs = installmgr
87
                                        .getInstalledPackages(pluginmgr.getPluginsFolder());
88
                        for (int i = 0; i < pkgs.length; i++) {
89
                                PackageInfo pkg = pkgs[i];
90
                                if ( !InstallerManager.ARCH.ALL.equalsIgnoreCase(pkg.getArchitecture()) && !installmgr.getArchitecture().equalsIgnoreCase(
91
                                                pkg.getArchitecture())) {
92
                                        mismatchs.add(pkg);
93
                                }
94
                                if (!InstallerManager.OS.ALL.equalsIgnoreCase(pkg.getOperatingSystem()) &&  !installmgr.getOperatingSystem().equalsIgnoreCase(
95
                                                pkg.getOperatingSystem())) {
96
                                        mismatchs.add(pkg);
97
                                }
98
                        }
99
                } catch (Throwable e) {
100
                        logger.info("Can't get installed packages.",e);
101
                }
102
                if( mismatchs.size() == 0  ) {
103
                        return;
104
                }
105

    
106
                StringBuffer buffer = new StringBuffer();
107
                Iterator<PackageInfo> it = mismatchs.iterator();
108
                while( it.hasNext() ) {
109
                        PackageInfo pkg = it.next();
110
            buffer.append(pkg.getName());
111
            buffer.append(" (");
112
            buffer.append(pkg.getOperatingSystem());
113
            buffer.append("/");
114
            buffer.append(pkg.getArchitecture());
115
            buffer.append(")");
116
                        buffer.append("<br>\n");
117
                }
118
                String template = "<html>"
119
                                + "<p>Packages are installed that are not compatible with your system.</p>\n"
120
                                + "<br>\n"
121
                                + "$(PACKAGES)"
122
                                + "<br>\n"
123
                                + "<p>Some are not specific to your system or architecture.</p>\n"
124
                                + "<br>\n" 
125
                                + "<p>You may need to configure a 32-bit Java environment\n"
126
                                + "for the proper functioning of gvSIG.</p>\n" 
127
                                + "</html>\n";
128

    
129
                try {
130
                        String fname = application.translate("filename_warning_architecture_or_os_mismatch");
131
                        URL res = this.getClass().getClassLoader().getResource(fname);
132
                        template = FileUtils.readFileToString(new File(res.getFile()));
133
                } catch (Throwable e) {
134
                        logger.info("Can't get template, use default.",e);
135
                }
136
                
137
                String msg = template.replaceAll("[$][(]PACKAGES[)]", buffer.toString());
138
                msg = msg.replaceAll("[%]PACKAGES[%]", buffer.toString());
139
                msg = msg.replaceAll("[$]PACKAGES", buffer.toString());
140
                
141
                application.showDialog(new ShowMessageControler(msg), "_Warning");
142
        }
143

    
144
        public class ShowMessageControler extends ShowMessageView {
145
                /**
146
                 * 
147
                 */
148
                private static final long serialVersionUID = 2641062720310466029L;
149

    
150
                public ShowMessageControler(String message) {
151
                        super();
152
                        this.messaje.setContentType("text/html");
153
                        this.messaje.setText(message);
154
                        this.closeButton.addActionListener(new ActionListener() {
155
                                public void actionPerformed(ActionEvent arg0) {
156
                                        close();
157
                                }
158
                        });
159
                        ApplicationManager application = ApplicationLocator.getManager();
160
                        this.closeButton.setText(application.translate("Close"));
161
                }
162
                
163
                public void close() {
164
                        this.setVisible(false);
165
                }
166
                
167
        }
168
        
169
        public class ShowMessageView extends JPanel {
170
                private static final long serialVersionUID = 8291970039773969840L;
171
                JTextPane messaje = new JTextPane();
172
                JButton closeButton = new JButton();
173

    
174
                /**
175
                 * Default constructor
176
                 */
177
                public ShowMessageView() {
178
                        initializePanel();
179
                }
180
                
181
                /**
182
                 * Adds fill components to empty cells in the first row and first column
183
                 * of the grid. This ensures that the grid spacing will be the same as
184
                 * shown in the designer.
185
                 * 
186
                 * @param cols
187
                 *            an array of column indices in the first row where fill
188
                 *            components should be added.
189
                 * @param rows
190
                 *            an array of row indices in the first column where fill
191
                 *            components should be added.
192
                 */
193
                private void addFillComponents(Container panel, int[] cols, int[] rows) {
194
                        Dimension filler = new Dimension(10, 10);
195

    
196
                        boolean filled_cell_11 = false;
197
                        CellConstraints cc = new CellConstraints();
198
                        if (cols.length > 0 && rows.length > 0) {
199
                                if (cols[0] == 1 && rows[0] == 1) {
200
                                        /** add a rigid area */
201
                                        panel.add(Box.createRigidArea(filler), cc.xy(1, 1));
202
                                        filled_cell_11 = true;
203
                                }
204
                        }
205

    
206
                        for (int index = 0; index < cols.length; index++) {
207
                                if (cols[index] == 1 && filled_cell_11) {
208
                                        continue;
209
                                }
210
                                panel.add(Box.createRigidArea(filler), cc.xy(cols[index], 1));
211
                        }
212

    
213
                        for (int index = 0; index < rows.length; index++) {
214
                                if (rows[index] == 1 && filled_cell_11) {
215
                                        continue;
216
                                }
217
                                panel.add(Box.createRigidArea(filler), cc.xy(1, rows[index]));
218
                        }
219

    
220
                }
221

    
222
                /**
223
                 * Method for recalculating the component orientation for right-to-left
224
                 * Locales.
225
                 * 
226
                 * @param orientation
227
                 *            the component orientation to be applied
228
                 */
229
                public void applyComponentOrientation(ComponentOrientation orientation) {
230
                        // Not yet implemented...
231
                        // I18NUtils.applyComponentOrientation(this, orientation);
232
                        super.applyComponentOrientation(orientation);
233
                }
234

    
235
                public JPanel createPanel() {
236
                        JPanel jpanel1 = new JPanel();
237
                        FormLayout formlayout1 = new FormLayout(
238
                                        "FILL:DEFAULT:NONE,FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE",
239
                                        "CENTER:DEFAULT:NONE,CENTER:DEFAULT:GROW(1.0),CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
240
                        CellConstraints cc = new CellConstraints();
241
                        jpanel1.setLayout(formlayout1);
242

    
243
                        JScrollPane jscrollpane1 = new JScrollPane();
244
                        jscrollpane1.setViewportView(messaje);
245
                        jscrollpane1
246
                                        .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
247
                        jscrollpane1
248
                                        .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
249
                        jpanel1.add(jscrollpane1, new CellConstraints(2, 2, 3, 1,
250
                                        CellConstraints.DEFAULT, CellConstraints.FILL));
251

    
252
                        closeButton.setActionCommand("JButton");
253
                        closeButton.setText("JButton");
254
                        jpanel1.add(closeButton, cc.xy(3, 4));
255

    
256
                        addFillComponents(jpanel1, new int[] { 1, 2, 3, 4, 5 }, new int[] {
257
                                        1, 2, 3, 4, 5 });
258
                        return jpanel1;
259
                }
260

    
261
                /**
262
                 * Initializer
263
                 */
264
                protected void initializePanel() {
265
                        setLayout(new BorderLayout());
266
                        add(createPanel(), BorderLayout.CENTER);
267
                }
268

    
269
        }
270

    
271
}