Changeset 2035

Show
Ignore:
Timestamp:
02/26/07 11:20:01 (2 years ago)
Author:
merlimat
Message:

EclipseTracPlugin:

Saving Trac servers preferences now works.
Fixes #1237.
Contributed by kompiro.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/models/TracServerList.java

    r2021 r2035  
    44package mm.eclipse.trac.models; 
    55 
    6 import java.net.MalformedURLException; 
     6import java.io.ByteArrayOutputStream; 
     7import java.io.File; 
     8import java.io.FileInputStream; 
     9import java.io.FileOutputStream; 
     10import java.io.IOException; 
     11import java.io.InputStream; 
    712import java.net.URL; 
    813import java.util.ArrayList; 
     
    1015import java.util.List; 
    1116 
     17import javax.xml.parsers.DocumentBuilder; 
     18import javax.xml.parsers.DocumentBuilderFactory; 
     19import javax.xml.parsers.ParserConfigurationException; 
     20import javax.xml.transform.OutputKeys; 
     21import javax.xml.transform.Transformer; 
     22import javax.xml.transform.TransformerException; 
     23import javax.xml.transform.TransformerFactory; 
     24import javax.xml.transform.dom.DOMSource; 
     25import javax.xml.transform.stream.StreamResult; 
     26 
    1227import mm.eclipse.trac.Activator; 
    1328import mm.eclipse.trac.Log; 
    1429 
    15 import org.eclipse.core.runtime.preferences.ConfigurationScope; 
    16 import org.eclipse.core.runtime.preferences.IEclipsePreferences; 
    17 import org.osgi.service.prefs.BackingStoreException; 
     30import org.eclipse.core.runtime.IPath; 
     31import org.w3c.dom.Document; 
     32import org.w3c.dom.Element; 
     33import org.w3c.dom.Node; 
     34import org.w3c.dom.NodeList; 
     35import org.xml.sax.InputSource; 
     36import org.xml.sax.SAXException; 
     37import org.xml.sax.helpers.DefaultHandler; 
    1838 
    1939/** 
     
    2545    private List<TracServer>    servers; 
    2646     
    27     private static final String CATEGORY = Activator.PLUGIN_ID + ".server"; 
    28      
    2947    public List<TracServer> getServers() 
    3048    { 
     
    4664    private TracServerList() 
    4765    { 
    48         savePreferences(); 
     66       servers = new ArrayList<TracServer>(); 
    4967        loadPreferences(); 
    5068         
    51         servers = new ArrayList<TracServer>(); 
    52         try 
    53         { 
    54             TracServer server = new TracServer( "Entorno.Tpvs", 
    55                                                 new URL( "http://entorno.tpvs/trac" ), 
    56                                                 "mme002es", "bjc2100", false ); 
    57             server.connect(); 
    58             servers.add( server ); 
    59              
    60             server = new TracServer( "Ubuntu", new URL( "http://ubuntu" ), "", 
    61                                      "", true ); 
    62             server.connect(); 
    63             servers.add( server ); 
    64              
    65         } catch ( MalformedURLException e ) 
    66         { 
    67         } 
    6869    } 
    6970     
    7071    private void loadPreferences() 
    7172    { 
    72         IEclipsePreferences prefs = new ConfigurationScope().getNode( CATEGORY ); 
     73                IPath libPath = Activator.getDefault().getStateLocation(); 
     74                libPath = libPath.append("servers.xml"); //$NON-NLS-1$ 
     75                File file = libPath.toFile(); 
     76                if (file.exists()) { 
     77                        try { 
     78                                InputStream stream = new FileInputStream(file); 
     79                                DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
     80                                parser.setErrorHandler(new DefaultHandler()); 
     81                                Element root = parser.parse(new InputSource(stream)).getDocumentElement(); 
     82                                if(!root.getNodeName().equals("serverInfos")) { //$NON-NLS-1$ 
     83                                        return; 
     84                                } 
     85                                 
     86                                NodeList list = root.getChildNodes(); 
     87                                int length = list.getLength(); 
     88                                for (int i = 0; i < length; ++i) { 
     89                                        Node node = list.item(i); 
     90                                        short type = node.getNodeType(); 
     91                                        if (type == Node.ELEMENT_NODE) { 
     92                                                Element element = (Element) node; 
     93                                                String nodeName = element.getNodeName(); 
     94                                                if (nodeName.equalsIgnoreCase("server")) { //$NON-NLS-1$ 
     95                                                        String name = element.getAttribute("name"); //$NON-NLS-1$ 
     96                                                        String url = element.getAttribute("url"); //$NON-NLS-1$ 
     97                                                        String username = element.getAttribute("username"); //$NON-NLS-1$ 
     98                                                        String password = element.getAttribute("password"); //$NON-NLS-1$ 
     99                                                        String anonymous = element.getAttribute("anonymous"); //$NON-NLS-1$ 
     100                                                        TracServer server = new TracServer(name,new URL(url),username,password,Boolean.parseBoolean(anonymous)); 
     101                                                        server.connect(); 
     102                                                        servers.add(server); 
     103                                                } 
     104                                        } 
     105                                } 
     106                                 
     107                        } catch (IOException e) { 
     108                                Log.error("Error is occured.", e); 
     109                        } catch (ParserConfigurationException e) { 
     110                                Log.error("Error is occured.", e); 
     111                        } catch (SAXException e) { 
     112                                Log.error("Error is occured.", e); 
     113                        } 
     114                } 
     115 
     116        Activator.getDefault().getStateLocation(); 
     117    } 
     118     
     119        /* 
     120     * (non-Javadoc) 
     121     *  
     122     * @see java.lang.Iterable#iterator() 
     123     */ 
     124    public Iterator<TracServer> iterator() 
     125    { 
     126        if ( servers == null ) return null; 
    73127         
    74         String[] keys; 
    75         try 
    76         { 
    77             keys = prefs.childrenNames(); 
    78         } catch ( BackingStoreException e ) 
    79         { 
    80             Log.error( "BackingStoreException.", e ); 
    81             keys = new String[0]; 
    82         } 
     128        return servers.iterator(); 
     129    } 
     130     
     131    public static TracServerList getInstance() 
     132    { 
     133        if ( instance == null ) instance = new TracServerList(); 
    83134         
    84         for ( String key : keys ) 
    85         { 
    86             IEclipsePreferences node = (IEclipsePreferences) prefs.node( key ); 
    87             String value = node.get( "test", "hola" ); 
    88             Log.info( "Test: " + value ); 
    89         } 
    90     } 
    91      
    92     private void savePreferences() 
    93     { 
     135        return instance; 
     136    } 
     137     
     138    private static TracServerList instance; 
     139     
     140    @Override 
     141    protected void notifyChanged() { 
     142        try { 
     143                        savePreferences(); 
     144                } catch (ParserConfigurationException e) { 
     145                } catch (IOException e) { 
     146                } catch (TransformerException e) { 
     147                } 
     148        super.notifyChanged(); 
     149    } 
     150    private void savePreferences() throws ParserConfigurationException, IOException, TransformerException 
     151    { 
     152                FileOutputStream stream= null; 
     153                try { 
     154                        String xml = getServerInfoAsXML(); 
     155                        IPath libPath = Activator.getDefault().getStateLocation(); 
     156                        libPath = libPath.append("servers.xml"); 
     157                        File file = libPath.toFile(); 
     158                        if (!file.exists()) { 
     159                                file.createNewFile(); 
     160                        } 
     161                        stream = new FileOutputStream(file); 
     162                        stream.write(xml.getBytes("UTF8")); //$NON-NLS-1$ 
     163                } catch (IOException e) { 
     164                        Log.error("exception is occured.",e); 
     165                } catch (ParserConfigurationException e) { 
     166                        Log.error("exception is occured.",e); 
     167                } catch (TransformerException e) { 
     168                        Log.error("exception is occured.",e); 
     169                } finally { 
     170                        if (stream != null) { 
     171                                try { 
     172                                        stream.close(); 
     173                                } catch (IOException e1) { 
     174                                } 
     175                        } 
     176                } 
     177 
     178                 
    94179    // Activator.getDefault().getPluginPreferences(). 
    95180    // IEclipsePreferences prefs = new ConfigurationScope().getNode( CATEGORY ); 
     
    107192    } 
    108193     
    109     /* 
    110      * (non-Javadoc) 
    111      *  
    112      * @see java.lang.Iterable#iterator() 
    113      */ 
    114     public Iterator<TracServer> iterator() 
    115     { 
    116         if ( servers == null ) return null; 
    117          
    118         return servers.iterator(); 
    119     } 
    120      
    121     public static TracServerList getInstance() 
    122     { 
    123         if ( instance == null ) instance = new TracServerList(); 
    124          
    125         return instance; 
    126     } 
    127      
    128     private static TracServerList instance; 
    129      
     194    private String getServerInfoAsXML() throws ParserConfigurationException, IOException, TransformerException { 
     195                Document doc = getDocument(); 
     196                Element config = doc.createElement("serverInfos");    //$NON-NLS-1$ 
     197                doc.appendChild(config); 
     198                for(TracServer server : TracServerList.getInstance()){ 
     199                        Element serverElement = doc.createElement("server");    //$NON-NLS-1$ 
     200                        serverElement.setAttribute("name", server.getName()); //$NON-NLS-1$ 
     201                        serverElement.setAttribute("url", server.getUrl().toString()); //$NON-NLS-1$ 
     202                        serverElement.setAttribute("username", server.getUsername()); //$NON-NLS-1$ 
     203                        serverElement.setAttribute("password", server.getPassword()); //$NON-NLS-1$ 
     204                        serverElement.setAttribute("anonymous", Boolean.toString(server.isAnonymous())); //$NON-NLS-1$ 
     205                        config.appendChild(serverElement); 
     206                } 
     207                return serializeDocument(doc); 
     208        } 
     209     
     210        private Document getDocument() throws ParserConfigurationException { 
     211                DocumentBuilderFactory dfactory= DocumentBuilderFactory.newInstance(); 
     212                DocumentBuilder docBuilder= dfactory.newDocumentBuilder(); 
     213                Document doc= docBuilder.newDocument(); 
     214                return doc; 
     215        } 
     216         
     217        private String serializeDocument(Document doc) throws IOException, TransformerException { 
     218                ByteArrayOutputStream s= new ByteArrayOutputStream(); 
     219                 
     220                TransformerFactory factory= TransformerFactory.newInstance(); 
     221                Transformer transformer= factory.newTransformer(); 
     222                transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$ 
     223                transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ 
     224                 
     225                DOMSource source= new DOMSource(doc); 
     226                StreamResult outputTarget= new StreamResult(s); 
     227                transformer.transform(source, outputTarget); 
     228                 
     229                return s.toString("UTF8"); //$NON-NLS-1$                         
     230        } 
     231 
    130232} 
  • eclipsetracplugin/eclipse/trunk/mm.eclipse.trac/src/mm/eclipse/trac/views/actions/TracServerActionProvider.java

    r2021 r2035  
    55 
    66import mm.eclipse.trac.models.TracServer; 
     7import mm.eclipse.trac.models.TracServerList; 
    78 
    89import org.eclipse.jface.action.Action; 
    910import org.eclipse.jface.action.IMenuManager; 
     11import org.eclipse.jface.action.Separator; 
    1012import org.eclipse.jface.viewers.IStructuredSelection; 
    1113import org.eclipse.jface.viewers.StructuredViewer; 
     
    2224    private Action           connectAction; 
    2325    private Action           disconnectAction; 
     26    private Action           deleteAction; 
    2427     
    2528    public TracServerActionProvider( StructuredViewer viewer ) 
     
    6972        }; 
    7073        disconnectAction.setText( "Disconnect from server" ); 
     74         
     75        deleteAction = new Action() { 
     76                @Override 
     77                public void run() { 
     78                if ( !viewer.getSelection().isEmpty() ) 
     79                { 
     80                    TracServer server = (TracServer) ((IStructuredSelection) viewer 
     81                            .getSelection()).getFirstElement(); 
     82                    TracServerList.getInstance().removeServer(server); 
     83                } 
     84                         
     85                } 
     86        }; 
     87         
     88        deleteAction.setText("Delete the Trac Server Setting"); 
    7189    } 
    7290     
     
    91109            menu.add( connectAction ); 
    92110            menu.add( disconnectAction ); 
     111            menu.add( new Separator() ); 
     112            menu.add( deleteAction ); 
    93113        } 
    94114    }