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 @ 42590

History | View | Annotate | Download (9.92 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 modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.extension;
24

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

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

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

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

    
58
public class CheckSOAndArquitectureExtension extends Extension implements Runnable {
59

    
60
    private static final Logger logger = LoggerFactory.getLogger(CheckSOAndArquitectureExtension.class);
61

    
62
    @Override
63
    public void initialize() {
64
        // Do nothing
65
    }
66

    
67
    @Override
68
    public void execute(String actionCommand) {
69
        // Do nothing
70

    
71
    }
72

    
73
    @Override
74
    public boolean isEnabled() {
75
        return false;
76
    }
77

    
78
    @Override
79
    public boolean isVisible() {
80
        return false;
81
    }
82

    
83
    @Override
84
    public void postInitialize() {
85
        PluginsManager pluginManager = PluginsLocator.getManager();
86
        pluginManager.addStartupTask("CheckOsAndArquitectre", this, true, 100);
87
    }
88

    
89
    @Override
90
    public void run() {
91
        ApplicationManager application = ApplicationLocator.getManager();
92
        InstallerManager installmgr = InstallerLocator.getInstallerManager();
93
        PluginsManager pluginmgr = PluginsLocator.getManager();
94

    
95
        Set<PackageInfo> mismatchs = new HashSet<>();
96
        try {
97
            PackageInfo[] pkgs = installmgr.getInstalledPackages();
98
            for (PackageInfo pkg : pkgs) {
99
                if (!InstallerManager.ARCH.ALL.equalsIgnoreCase(pkg.getArchitecture()) && !installmgr.getArchitecture().equalsIgnoreCase(
100
                        pkg.getArchitecture())) {
101
                    mismatchs.add(pkg);
102
                }
103
                if (!InstallerManager.OS.ALL.equalsIgnoreCase(pkg.getOperatingSystem()) && !installmgr.getOperatingSystem().equalsIgnoreCase(
104
                        pkg.getOperatingSystem())) {
105
                    mismatchs.add(pkg);
106
                }
107
            }
108
        } catch (Throwable e) {
109
            logger.info("Can't get installed packages.", e);
110
        }
111
        if (mismatchs.isEmpty()) {
112
            return;
113
        }
114

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

    
138
        try {
139
            String fname = "i18n/" + application.translate("_filename_warning_architecture_or_os_mismatch");
140
            URL res = this.getClass().getClassLoader().getResource(fname);
141
            template = FileUtils.readFileToString(new File(res.getFile()));
142
        } catch (Throwable e) {
143
            logger.info("Can't get template, use default.", e);
144
        }
145

    
146
        String msg = template.replaceAll("[$][(]PACKAGES[)]", buffer.toString());
147
        msg = msg.replaceAll("[%]PACKAGES[%]", buffer.toString());
148
        msg = msg.replaceAll("[$]PACKAGES", buffer.toString());
149

    
150
        application.showDialog(new ShowMessageControler(msg), "_Warning");
151
    }
152

    
153
    public class ShowMessageControler extends ShowMessageView {
154

    
155
        /**
156
         *
157
         */
158
        private static final long serialVersionUID = 2641062720310466029L;
159

    
160
        public ShowMessageControler(String message) {
161
            super();
162
            this.messaje.setContentType("text/html");
163
            this.messaje.setText(message);
164
            this.closeButton.addActionListener(new ActionListener() {
165
                @Override
166
                public void actionPerformed(ActionEvent arg0) {
167
                    close();
168
                }
169
            });
170
            ApplicationManager application = ApplicationLocator.getManager();
171
            this.closeButton.setText(application.translate("Close"));
172
        }
173

    
174
        public void close() {
175
            this.setVisible(false);
176
        }
177

    
178
    }
179

    
180
    public class ShowMessageView extends JPanel {
181

    
182
        private static final long serialVersionUID = 8291970039773969840L;
183
        JTextPane messaje = new JTextPane();
184
        JButton closeButton = new JButton();
185

    
186
        /**
187
         * Default constructor
188
         */
189
        public ShowMessageView() {
190
            initializePanel();
191
        }
192

    
193
        /**
194
         * Adds fill components to empty cells in the first row and first column
195
         * of the grid. This ensures that the grid spacing will be the same as
196
         * shown in the designer.
197
         *
198
         * @param cols an array of column indices in the first row where fill
199
         * components should be added.
200
         * @param rows an array of row indices in the first column where fill
201
         * components should be added.
202
         */
203
        private void addFillComponents(Container panel, int[] cols, int[] rows) {
204
            Dimension filler = new Dimension(10, 10);
205

    
206
            boolean filled_cell_11 = false;
207
            CellConstraints cc = new CellConstraints();
208
            if (cols.length > 0 && rows.length > 0) {
209
                if (cols[0] == 1 && rows[0] == 1) {
210
                    /**
211
                     * add a rigid area
212
                     */
213
                    panel.add(Box.createRigidArea(filler), cc.xy(1, 1));
214
                    filled_cell_11 = true;
215
                }
216
            }
217

    
218
            for (int index = 0; index < cols.length; index++) {
219
                if (cols[index] == 1 && filled_cell_11) {
220
                    continue;
221
                }
222
                panel.add(Box.createRigidArea(filler), cc.xy(cols[index], 1));
223
            }
224

    
225
            for (int index = 0; index < rows.length; index++) {
226
                if (rows[index] == 1 && filled_cell_11) {
227
                    continue;
228
                }
229
                panel.add(Box.createRigidArea(filler), cc.xy(1, rows[index]));
230
            }
231

    
232
        }
233

    
234
        /**
235
         * Method for recalculating the component orientation for right-to-left
236
         * Locales.
237
         *
238
         * @param orientation the component orientation to be applied
239
         */
240
        @Override
241
        public void applyComponentOrientation(ComponentOrientation orientation) {
242
                        // Not yet implemented...
243
            // I18NUtils.applyComponentOrientation(this, orientation);
244
            super.applyComponentOrientation(orientation);
245
        }
246

    
247
        public JPanel createPanel() {
248
            JPanel jpanel1 = new JPanel();
249
            FormLayout formlayout1 = new FormLayout(
250
                    "FILL:DEFAULT:NONE,FILL:DEFAULT:GROW(1.0),FILL:DEFAULT:NONE,FILL:DEFAULT:NONE,FILL:DEFAULT:NONE",
251
                    "CENTER:DEFAULT:NONE,CENTER:DEFAULT:GROW(1.0),CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE,CENTER:DEFAULT:NONE");
252
            CellConstraints cc = new CellConstraints();
253
            jpanel1.setLayout(formlayout1);
254

    
255
            JScrollPane jscrollpane1 = new JScrollPane();
256
            jscrollpane1.setViewportView(messaje);
257
            jscrollpane1
258
                    .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
259
            jscrollpane1
260
                    .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
261
            jpanel1.add(jscrollpane1, new CellConstraints(2, 2, 3, 1,
262
                    CellConstraints.DEFAULT, CellConstraints.FILL));
263

    
264
            closeButton.setActionCommand("JButton");
265
            closeButton.setText("JButton");
266
            jpanel1.add(closeButton, cc.xy(3, 4));
267

    
268
            addFillComponents(jpanel1, new int[]{1, 2, 3, 4, 5}, new int[]{
269
                1, 2, 3, 4, 5});
270
            return jpanel1;
271
        }
272

    
273
        /**
274
         * Initializer
275
         */
276
        protected void initializePanel() {
277
            setLayout(new BorderLayout());
278
            add(createPanel(), BorderLayout.CENTER);
279
        }
280

    
281
    }
282

    
283
}