Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 652

History | View | Annotate | Download (5.83 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.awt.Component;
4
import java.io.File;
5
import java.io.FileNotFoundException;
6
import java.io.FileReader;
7
import java.io.FileWriter;
8
import java.text.DateFormat;
9
import java.util.ArrayList;
10
import java.util.Date;
11

    
12
import javax.swing.JFileChooser;
13
import javax.swing.JOptionPane;
14

    
15
import org.exolab.castor.xml.MarshalException;
16
import org.exolab.castor.xml.Marshaller;
17
import org.exolab.castor.xml.ValidationException;
18

    
19
import com.iver.andami.PluginServices;
20
import com.iver.andami.plugins.Extension;
21
import com.iver.cit.gvsig.fmap.DriverException;
22
import com.iver.cit.gvsig.fmap.layers.XMLException;
23
import com.iver.cit.gvsig.project.ProjectFactory;
24
import com.iver.cit.gvsig.project.ProjectWindow;
25
import com.iver.cit.gvsig.project.castor.Project;
26
import com.iver.utiles.GenericFileFilter;
27
import com.iver.utiles.XMLEntity;
28
import com.iver.utiles.xmlEntity.generate.XmlTag;
29

    
30
/**
31
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
32
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
33
 * en esta clase.
34
 *
35
 * @author Fernando Gonz?lez Cort?s
36
 */
37
public class ProjectExtension implements Extension {
38
    private ArrayList tableExtensions = new ArrayList();
39
    private ProjectWindow projectFrame;
40
    private Project p;
41
        private boolean modified = false;
42

    
43
    /**
44
     * @see com.iver.mdiApp.plugins.Extension#inicializar()
45
     */
46
    public void inicializar() {
47
        p = ProjectFactory.createProject();
48
        p.setName(PluginServices.getText(this, "untitled"));
49
        p.setModified(false);
50
        projectFrame = new ProjectWindow(this);
51
        projectFrame.setProject(p);
52
        showProjectWindow();
53
        PluginServices.getMainFrame().setTitle("gvSIG: " + PluginServices.getText(this,
54
                                                                         "sin_titulo"));
55
    }
56

    
57
        public void showProjectWindow(){
58
                PluginServices.getMDIManager().addView(projectFrame);
59
        }
60

    
61
        private void guardar(){
62
                if (p.getPath() == null) {
63
                        JFileChooser jfc = new JFileChooser();
64
                        jfc.addChoosableFileFilter(new GenericFileFilter("xml", "tipo_fichero_proyecto"));
65
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
66
                                escribirProyecto(jfc.getSelectedFile(), p);
67
                        }
68
                } else {
69
                        escribirProyecto(new File(p.getPath()), p);
70
                }
71
        }
72
        
73
        private boolean modificado(){
74
                if (p.isModified()) {
75
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
76
                                        PluginServices.getText(this, "guardar_cambios"),
77
                                        PluginServices.getText(this, "nuevo_proyecto"),
78
                                        JOptionPane.YES_NO_CANCEL_OPTION,
79
                                        JOptionPane.INFORMATION_MESSAGE);
80

    
81
                        if (res == JOptionPane.YES_OPTION) {
82
                                guardar();
83
                        } else if (res == JOptionPane.CANCEL_OPTION) {
84
                                return false;
85
                        }
86
                }
87
                
88
                return true;
89
        }
90

    
91
    /**
92
     * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
93
     */
94
    public void execute(String actionCommand) {
95
            if (actionCommand.equals("NUEVO")) {
96
                    //Si est? modificado se pregunta si se quiere guardar el anterior
97
                                if (!modificado()) return;
98

    
99
                                PluginServices.getMDIManager().closeAllViews();
100
                p = ProjectFactory.createProject();
101
                p.setName(PluginServices.getText(this, "untitled"));
102
                p.setModified(false);
103
                projectFrame.setProject(p);
104
                showProjectWindow();
105
                PluginServices.getMainFrame().setTitle("gvSIG: " + PluginServices.getText(this,
106
                                 "sin_titulo"));
107

    
108
            } else if (actionCommand.equals("ABRIR")) {
109
                                //Si est? modificado se pregunta si se quiere guardar el anterior
110
                                if (!modificado()) return;
111

    
112
                                JFileChooser jfc = new JFileChooser();
113
                                jfc.addChoosableFileFilter(new GenericFileFilter("xml", "tipo_fichero_proyecto"));
114
                                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION){
115
                                        PluginServices.getMDIManager().closeAllViews();
116
                        Project o = leerProyecto(jfc.getSelectedFile());
117
                        if (o != null){
118
                                                p = o; 
119
                        }
120
                        projectFrame.setProject(p);
121
                        projectFrame.refreshControls();
122
                        showProjectWindow();
123
                                }
124
            } else if (actionCommand.equals("GUARDAR")) {
125
                    guardar();
126
            }
127
    }
128

    
129
    private void escribirProyecto(File file, Project p){
130
            p.setPath(file.toString());
131
            p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
132
            p.setModified(false);
133

    
134
            // write it out as XML
135
            try {
136
                    FileWriter writer = new FileWriter(file.getAbsolutePath());
137
                    Marshaller m = new Marshaller(writer);
138
                    m.setEncoding("ISO-8859-1");
139
                    m.marshal(p.getXMLEntity().getXmlTag());
140
            } catch (Exception e) {
141
                System.out.println("Exception marshal Proyecto " + e);
142
            }
143
            PluginServices.getMainFrame().setTitle("gvSIG: " + file.getName());
144
            projectFrame.refreshControls();
145
    }
146

    
147
    /**
148
     * @see com.iver.mdiApp.FileExtension#writeFile(java.io.File)
149
     */
150
    public Project leerProyecto(File file) {
151
            Project proj = null;
152
                try {
153
                 File xmlFile = new File(file.getAbsolutePath());
154
                 FileReader reader;
155
                                reader = new FileReader(xmlFile);
156
                                XmlTag tag=(XmlTag)XmlTag.unmarshal(reader);
157
                        p=Project.createFromXML(new XMLEntity(tag));
158
                        } catch (FileNotFoundException e) {
159
                        } catch (MarshalException e) {
160
                        } catch (ValidationException e) {
161
                        } catch (XMLException e) {
162
                        } catch (DriverException e) {
163
                        }
164
        
165
        return proj;
166
    }
167

    
168
    /**
169
         * @return
170
         */
171
        public Project getProject() {
172
                return p;
173
        }
174

    
175
        /**
176
         * @see com.iver.andami.plugins.Extension#isEnabled()
177
         */
178
        public boolean isEnabled() {
179
                return true;
180
        }
181

    
182
        /**
183
         * @see com.iver.andami.plugins.Extension#isVisible()
184
         */
185
        public boolean isVisible() {
186
                return true;
187
        }
188

    
189
}