Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableOperations.java @ 931

History | View | Annotate | Download (5.11 KB)

1 312 fernando
package com.iver.cit.gvsig;
2
3 884 fernando
import java.io.IOException;
4 855 fernando
5 884 fernando
import com.hardcode.driverManager.DriverLoadException;
6
import com.hardcode.gdbms.engine.data.DataSource;
7
import com.hardcode.gdbms.engine.data.DataSourceFactory;
8
import com.hardcode.gdbms.engine.instruction.SemanticException;
9
import com.hardcode.gdbms.parser.ParseException;
10 855 fernando
import com.iver.andami.PluginServices;
11
import com.iver.andami.messages.NotificationManager;
12 596 fernando
import com.iver.andami.plugins.Extension;
13 855 fernando
import com.iver.andami.ui.mdiManager.View;
14
import com.iver.cit.gvsig.fmap.DriverException;
15 884 fernando
import com.iver.cit.gvsig.fmap.layers.FBitSet;
16 855 fernando
import com.iver.cit.gvsig.fmap.layers.FLayer;
17
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
18
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
19 312 fernando
import com.iver.cit.gvsig.gui.Table;
20 855 fernando
import com.iver.cit.gvsig.gui.filter.DefaultExpressionDataSource;
21
import com.iver.cit.gvsig.gui.filter.ExpressionListener;
22
import com.iver.cit.gvsig.gui.filter.FilterDialog;
23
import com.iver.cit.gvsig.project.castor.ProjectView;
24
import com.iver.utiles.exceptionHandling.ExceptionListener;
25 312 fernando
26
27
/**
28
 * DOCUMENT ME!
29
 *
30
 * @author Fernando Gonz?lez Cort?s
31
 */
32 596 fernando
public class TableOperations implements Extension, ExpressionListener {
33 855 fernando
        private SelectableDataSource dataSource = null;
34
    private Table vista;
35 312 fernando
36
    /**
37
     * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
38
     */
39 596 fernando
    public void execute(String actionCommand) {
40 855 fernando
        View v = PluginServices.getMDIManager().getActiveView();
41
        if (v instanceof Table){
42
                   vista = (Table) v;
43
                    dataSource = vista.getModel().getModelo();
44
        }else if (v instanceof com.iver.cit.gvsig.gui.View){
45
                ProjectView pv = ((com.iver.cit.gvsig.gui.View) v).getModel();
46
                try {
47
                                dataSource = ((AlphanumericData)pv.getMapContext().getLayers().getActives()[0]).getRecordset();
48
                        } catch (DriverException e) {
49
                                NotificationManager.addError(e.getMessage(), e);
50
                        }
51
52 312 fernando
        }
53 855 fernando
54
                DefaultExpressionDataSource ds = new DefaultExpressionDataSource();
55 312 fernando
        ds.setTable(dataSource);
56
57 855 fernando
        FilterDialog dlg = new FilterDialog();
58 312 fernando
        dlg.addExpressionListener(this);
59 855 fernando
        dlg.addExceptionListener(new ExceptionListener() {
60
                        public void exceptionThrown(Throwable t) {
61
                                NotificationManager.addError(t.getMessage(), t);
62
                        }
63
                });
64 312 fernando
65
        dlg.setModel(ds);
66 855 fernando
        PluginServices.getMDIManager().addView(dlg);
67 312 fernando
    }
68
69
    /**
70 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
71 312 fernando
         */
72
        public void newSet(String expression) {
73 884 fernando
                long[] sel = doSet(expression);
74
75
                FBitSet selection = new FBitSet();
76
                for (int i = 0; i < sel.length; i++) {
77
                        selection.set((int) sel[i]);
78
                }
79
80 312 fernando
                dataSource.clearSelection();
81 884 fernando
                dataSource.setSelection(selection);
82 312 fernando
        }
83
84
        /**
85 855 fernando
         * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#newSet(java.lang.String)
86 312 fernando
         */
87 884 fernando
        private long[] doSet(String expression) {
88
                try {
89
                        DataSource ds = DataSourceFactory.executeSQL(expression);
90
                        return ds.getWhereFilter();
91
                } catch (DriverLoadException e) {
92
                        NotificationManager.addError("Error cargando el driver", e);
93
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
94
                        NotificationManager.addError("Error accediendo al driver", e);
95
                } catch (ParseException e) {
96
                        NotificationManager.addError("Parse error", e);
97
                } catch (SemanticException e) {
98
                        NotificationManager.addError(e.getMessage(), e);
99
                } catch (IOException e) {
100
                        NotificationManager.addError("GDBMS internal error", e);
101 312 fernando
                }
102 884 fernando
103
                return null;
104 312 fernando
        }
105
106
    /**
107 855 fernando
     * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#addToSet(java.lang.String)
108 312 fernando
     */
109
    public void addToSet(String expression) {
110 884 fernando
                long[] sel = doSet(expression);
111
112
                FBitSet selection = new FBitSet();
113
                for (int i = 0; i < sel.length; i++) {
114
                        selection.set((int) sel[i]);
115
                }
116
117
                FBitSet fbs = dataSource.getSelection();
118
                fbs.or(selection);
119
                dataSource.setSelection(fbs);
120 312 fernando
    }
121
122
    /**
123 855 fernando
     * @see com.iver.cit.gvsig.gui.filter.ExpressionListener#fromSet(java.lang.String)
124 312 fernando
     */
125
    public void fromSet(String expression) {
126 884 fernando
                long[] sel = doSet(expression);
127
128
                FBitSet selection = new FBitSet();
129
                for (int i = 0; i < sel.length; i++) {
130
                        selection.set((int) sel[i]);
131 312 fernando
                }
132 884 fernando
133
                FBitSet fbs = dataSource.getSelection();
134
                fbs.and(selection);
135
                dataSource.setSelection(fbs);
136 312 fernando
    }
137
138
        /**
139
         * @see com.iver.mdiApp.plugins.Extension#isVisible()
140
         */
141
        public boolean isVisible() {
142 855 fernando
                View v = PluginServices.getMDIManager().getActiveView();
143 312 fernando
144 855 fernando
                if (v == null) {
145 312 fernando
                        return false;
146
                }
147
148 855 fernando
                if (v.getClass() == Table.class) {
149 312 fernando
                        return true;
150
                }else{
151 855 fernando
                        if (v instanceof com.iver.cit.gvsig.gui.View){
152
                                com.iver.cit.gvsig.gui.View view = (com.iver.cit.gvsig.gui.View) v;
153
                                ProjectView pv = view.getModel();
154
                                FLayer[] seleccionadas = pv.getMapContext().getLayers().getActives();
155
                                if (seleccionadas.length == 1){
156
                                        if (seleccionadas[0] instanceof AlphanumericData){
157
                                                return true;
158
                                        }
159 312 fernando
                                }
160
                        }
161
                        return false;
162
                }
163
        }
164
165 596 fernando
        /**
166
         * @see com.iver.andami.plugins.Extension#inicializar()
167
         */
168
        public void inicializar() {
169
        }
170
171
        /**
172
         * @see com.iver.andami.plugins.Extension#isEnabled()
173
         */
174
        public boolean isEnabled() {
175 855 fernando
                return true;
176 596 fernando
        }
177
178 312 fernando
}