001/* [{
002Copyright 2008 Nicolas Carranza <nicarran at gmail.com>
003
004This file is part of jpen.
005
006jpen is free software: you can redistribute it and/or modify
007it under the terms of the GNU Lesser General Public License as published by
008the Free Software Foundation, either version 3 of the License,
009or (at your option) any later version.
010
011jpen is distributed in the hope that it will be useful,
012but WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014GNU Lesser General Public License for more details.
015
016You should have received a copy of the GNU Lesser General Public License
017along with jpen.  If not, see <http://www.gnu.org/licenses/>.
018}] */
019package jpen.demo;
020
021import java.util.Arrays;
022import java.util.Collection;
023import java.util.HashSet;
024import java.util.Set;
025import java.util.TreeSet;
026import jpen.PenDevice;
027import jpen.PenManager;
028import jpen.PenProvider;
029import jpen.PKind;
030import jpen.internal.ThrowableUtils;
031
032public class StatusReport{
033
034        private final StringBuilder report=new StringBuilder();
035
036        public StatusReport(PenManager penManager){
037                appendHeader(penManager);
038                appendPenThreadCrashInfo(penManager);
039                appendProvidersInfo(penManager);
040                appendSystemInfo(penManager);
041                appendFooter(penManager);
042        }
043
044        private void appendLine(String line){
045                appendLine(line, 0);
046        }
047
048        private void appendLine(String line, int indent){
049                line=line.trim();
050                for(int i=indent; --i>=0;)
051                        report.append("   ");
052                report.append(line);
053                if(!line.endsWith("\n"))
054                        report.append("\n");
055        }
056
057        private void appendHeader(PenManager penManager){
058                appendLine("===== JPen - Status Report =====");
059                appendLine("JPen Version: "+PenManager.getJPenFullVersion());
060                appendLine("Date: "+new java.util.Date());
061        }
062
063        private void appendPenThreadCrashInfo(PenManager penManager){
064                Exception penThreadCrashException=penManager.pen.getThreadException();
065                if(penThreadCrashException!=null){
066                        appendLine("Pen Thread Crashed: "+ThrowableUtils.evalStackTraceString(penThreadCrashException));
067                }
068        }
069
070        private void appendFooter(PenManager penManager){
071                appendLine("===== ===== =====");
072        }
073
074        private static final Set<String> PRIVATE_SYSTEM_PROPERTIES=new HashSet<String>(Arrays.asList(
075                                new String[]{
076                                        "user.dir",
077                                        "java.io.tmpdir",
078                                        "line.separator",
079                                        "user.home",
080                                        "user.name",
081                                }
082                        ));
083
084        private void appendSystemInfo(PenManager penManager){
085                appendLine("System Properties:");
086                try{
087                        TreeSet<Object> properties=new TreeSet<Object>(System.getProperties().keySet());
088                        for(Object property: properties){
089                                String propertyName=property.toString();
090                                if(PRIVATE_SYSTEM_PROPERTIES.contains(propertyName))
091                                        continue;
092                                appendLine(propertyName+": "+System.getProperty(propertyName), 1);
093                        }
094                }catch(SecurityException ex){
095                        appendLine("Security Exception: system properties are not readable.", 1);
096                }
097        }
098
099        private void appendProvidersInfo(PenManager penManager){
100                appendLine("Providers:");
101                for(PenProvider.Constructor constructor: penManager.getProviderConstructors()){
102                        appendLine("Constructor: "+constructor.getName(), 1);
103                        PenProvider.ConstructionException constructionException=constructor.getConstructionException();
104                        String constructionExceptionStackTrace="none";
105                        if(constructionException!=null){
106                                constructionExceptionStackTrace=ThrowableUtils.evalStackTraceString(constructionException);
107                        }
108                        appendLine("Construction Exception: "+constructionExceptionStackTrace, 2);
109                        PenProvider penProvider=constructor.getConstructed();
110                        if(penProvider!=null){
111                                int nativeVersion=constructor.getNativeVersion();
112                                if(nativeVersion!=-1){
113                                        appendLine("Native Version-Build(Expected): "+
114                                                                                 constructor.getNativeVersion()+"-"+constructor.getNativeBuild()+
115                                                                                 "("+constructor.getExpectedNativeBuild()+")", 2);
116                                }
117                                Collection<PenDevice> penDevices=penProvider.getDevices();
118                                for(PenDevice penDevice:penDevices){
119                                        appendLine(
120                                                (penManager.isSystemMouseDevice(penDevice)? "Device (System Mouse): ":"Device: ")+
121                                                penDevice.getName()+" ("+penDevice.getPhysicalId()+")", 2);
122                                        appendLine("Enabled: "+penDevice.getEnabled(), 3);
123                                        appendLine("Kind: "+PKind.valueOf(penDevice.getKindTypeNumber()), 3);
124                                        appendLine("Fractional Movements: "+penDevice.getUseFractionalMovements(), 3);
125                                }
126                        }
127                }
128        }
129
130        @Override
131        public String toString(){
132                return report.toString();
133        }
134
135}