001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 * Created on Mar 18, 2010
021 * Author: Andreas Prlic
022 *
023 */
024
025package org.biojava.nbio.structure.align.gui;
026
027import org.biojava.nbio.structure.align.webstart.BrowserOpener;
028
029import javax.swing.*;
030import javax.swing.border.EtchedBorder;
031import javax.swing.event.HyperlinkEvent;
032import javax.swing.event.HyperlinkListener;
033import java.awt.*;
034import java.awt.event.ActionEvent;
035import java.awt.event.ActionListener;
036import java.util.Enumeration;
037import java.util.Properties;
038import java.util.StringTokenizer;
039
040public class SystemInfo
041{
042
043        Box vBox;
044        String msg;
045
046        /*
047         * This is a default list of system properties that we will use
048         * if the Security Manager doesn't let us extract the "real" list.
049         */
050        public static final String defaultProperties =
051                ""
052                + "browser "
053                + "file.separator "
054                + "java.class.version "
055                + "java.vendor "
056                + "java.vendor.url "
057                + "java.version "
058                + "line.separator "
059                + "os.arch "
060                + "os.name "
061                + "os.version "
062                + "path.separator ";
063
064        public static final String hexPropertyNames =
065                " file.separator "
066                + "line.separator "
067                + "path.separator ";
068        public static final String urlPropertyNames =
069                " browser.vendor.url "
070                + " java.class.path "
071                + "java.home "
072                + "user.dir "
073                + "user.home "
074                + "user.name ";
075        EtchedBorder        border;
076
077        public SystemInfo()
078        {
079                border = new EtchedBorder();
080                msg = "";
081                try {
082                        Properties props = System.getProperties();
083                        /*
084                         * Unfortunately, enumerating System.getProperties() returns
085                         * them in an unsatisfactory order. To make the display
086                         * esthetically pleasing, we'll extract the property names
087                         * (i.e. the keys) into a vector, then sort the vector, then
088                         * use the vector as an enumeration. props.size() is not
089                         * trustworthy (bug in MRJ?)
090                         */
091                        //border.setLabelText ("System Properties");
092                        /*
093                         * Count the actual size of the System property list.
094                         */
095                        int size = 0;
096                        Enumeration<?> enumo = props.propertyNames();
097                        while (enumo.hasMoreElements()) {
098                                ++size;
099                                enumo.nextElement();
100                        }
101                        String[] names = new String[size];
102                        enumo = props.propertyNames();
103                        for (int i = 0; enumo.hasMoreElements(); i++) {
104                                names[i] = (String) enumo.nextElement();
105                        }
106                        if (size < 1) {
107                                msg = "No System Properties";
108                        }
109                        else {
110                                quickSort(names, 0, names.length - 1);
111                                for (int i = 0; i < size; i++) {
112                                        addOneSystemProperty(names[i]);
113                                }
114                        }
115                }
116                catch (SecurityException e) {
117                  // border.setLabelText ("Default Applet Properties");
118                        StringTokenizer t = new StringTokenizer(defaultProperties, " ");
119                        while (t.hasMoreElements()) {
120                                addOneSystemProperty(t.nextToken());
121                        }
122                }
123                catch (Exception e) {
124                        append("Strange Exception getting system properties: " + e);
125                }
126        }
127
128        private void append(String txt){
129                msg += txt;
130        }
131
132        /**
133         * Stripped-down QuickSort.
134         * @param   vector      The vector of strings to sort
135         * @param   startIndex  The first element to sort
136         * @param   endIndex    The last element to sort
137         *
138         * example use:
139         * <pre>
140         *    JavaInfo.quickSort(vector, 0, vector.length - 1);
141         *  </pre>
142         */
143        public static void quickSort(
144                          String[]        vector,
145                          int             startIndex,
146                          int             endIndex
147                 )
148        {
149                 int     i       = startIndex;
150                 int     j       = endIndex;
151                 String  pivot   = vector[(i + j) / 2];
152                 do {
153                          while (i < endIndex && pivot.compareTo(vector[i]) > 0) {
154                                        ++i;
155                          }
156                          while (j > startIndex && pivot.compareTo(vector[j]) < 0) {
157                                        --j;
158                          }
159                          if (i < j) {
160                                        String  temp    = vector[i];
161                                        vector[i]       = vector[j];
162                                        vector[j]       = temp;
163                          }
164                          if (i <= j) {
165                                        ++i;
166                                        --j;
167                          }
168                 } while (i <= j);
169                 if (startIndex < j) {
170                          quickSort(vector, startIndex, j);
171                 }
172                 if (i < endIndex) {
173                          quickSort(vector, i, endIndex);
174                 }
175        }
176
177
178        public void addOneSystemProperty(
179                        String          name
180        )
181        {
182                try {
183                        String propValue    = System.getProperty(name);
184                        /*
185                         * On the Macintosh (under MRJ), a bunch of font names
186                         * are loaded into the property list. We toss them
187                         * to avoid confusion.
188                         */
189                        if (propValue != null && !name.equals(propValue)) {
190                                append("<b>"+name + "</b>:");
191                                boolean isReadable = true;
192                                for (int i = 0; i < propValue.length(); i++) {
193                                        char    c   = propValue.charAt(i);
194                                        if (isControlCharacter(c)) {
195                                                isReadable = false;
196                                                break;
197                                        }
198                                }
199                                if (!isReadable) {
200                                        for (int i = 0; i < propValue.length(); i++) {
201                                                char    c   = propValue.charAt(i);
202                                                if (Character.isLetterOrDigit(c)) {
203                                                        append(" '" + c + "'");
204                                                }
205                                                else {
206                                                        append(" 0x");
207                                                        if (c < 0x10) {
208                                                                append("0");
209                                                        }
210                                                        append(Integer.toHexString(c));
211                                                }
212                                        }
213                                }
214                                else if (isURLProperty(name)) {
215                                        StringBuffer    fixed = new StringBuffer();
216                                        int     start   = 0;
217                                        int     hit     = 0;
218                                        while ((hit = propValue.indexOf('%', start)) >= 0) {
219                                                fixed.append(propValue.substring(start, hit));
220                                                int     value   =
221                                                        Integer.parseInt(propValue.substring(hit + 1, hit + 3), 16);
222                                                fixed.append(((char) value));
223                                                start = hit + 3;
224                                        }
225                                        append(" \"" + fixed + "\"");
226                                }
227                                else {
228                                        append(" " + propValue);
229                                }
230                                append("<br/>");
231                        } /* If the property name was found */
232                } /* Try to fetch the property name */
233                catch (SecurityException e) {
234                        append(name + ": Security Exception\n"  );
235                }
236                catch (Exception e) {
237                        append(name     + ": " + e + "\n");
238                }
239        }
240        protected boolean isHexProperty(
241                        String          thisName
242        )
243        {
244                int index   = hexPropertyNames.indexOf(" " + thisName + " ");
245                return (index >= 0);
246        }
247        protected boolean isURLProperty(
248                        String          thisName
249        )
250        {
251                int index   = urlPropertyNames.indexOf(" " + thisName + " ");
252                return (index >= 0);
253        }
254        /**
255         * Replicate the Java 1.1 isISOControl method (for Java 1.0.2 compatibility)
256         */
257        private boolean isControlCharacter(
258                        char            c
259        )
260        {
261                return ((c >= '\u0000' && c <= '\u001f')
262                                || (c >= '\u007f' && c <= '\u009f'));
263        }
264
265        public String getMessage(){
266                return msg;
267        }
268
269        public void showDialog(){
270                JDialog dialog = new JDialog();
271
272                dialog.setSize(new Dimension(500,650));
273
274                String msg = getMessage();
275                JEditorPane txt = new JEditorPane("text/html", msg);
276                txt.setEditable(false);
277
278                JScrollPane scroll = new JScrollPane(txt);
279                scroll.setSize(new Dimension(300,500));
280                vBox= Box.createVerticalBox();
281                vBox.add(scroll);
282
283                txt.addHyperlinkListener(new HyperlinkListener(){
284
285                        @Override
286                public void hyperlinkUpdate(HyperlinkEvent e) {
287
288                                if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
289                                        String href = e.getDescription();
290                                        BrowserOpener.showDocument(href);
291                                }
292                                if ( e.getEventType() == HyperlinkEvent.EventType.ENTERED) {
293                                        // change the mouse curor
294                                        vBox.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
295                                }
296                                if (e.getEventType() == HyperlinkEvent.EventType.EXITED) {
297                                        vBox.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
298                                }
299                        }
300                });
301
302
303
304
305                JButton close = new JButton("Close");
306
307                close.addActionListener(new ActionListener(){
308                        @Override
309                public void actionPerformed(ActionEvent event) {
310                                Object source = event.getSource();
311
312                                JButton but = (JButton)source;
313                                Container parent = but.getParent().getParent().getParent().getParent().getParent().getParent() ;
314
315                                JDialog dia = (JDialog) parent;
316                                dia.dispose();
317                        }
318                });
319
320                Box hBoxb = Box.createHorizontalBox();
321                hBoxb.add(Box.createGlue());
322                hBoxb.add(close,BorderLayout.EAST);
323
324                vBox.add(hBoxb);
325
326                dialog.getContentPane().add(vBox);
327                dialog.setVisible(true);
328
329        }
330}