Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableFieldOperations.java @ 27352

History | View | Annotate | Download (3.72 KB)

1 2241 fernando
package com.iver.cit.gvsig;
2
3
import java.io.IOException;
4
import java.util.BitSet;
5
6
import com.hardcode.driverManager.DriverLoadException;
7 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
8 2241 fernando
import com.hardcode.gdbms.engine.data.DataSource;
9
import com.hardcode.gdbms.engine.data.DataSourceFactory;
10
import com.hardcode.gdbms.engine.instruction.EvaluationException;
11
import com.hardcode.gdbms.engine.instruction.SemanticException;
12
import com.hardcode.gdbms.parser.ParseException;
13
import com.iver.andami.PluginServices;
14
import com.iver.andami.messages.NotificationManager;
15
import com.iver.andami.plugins.Extension;
16 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
17 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Table;
18 2241 fernando
19
/**
20 6010 caballero
 * @author Fernando Gonz?lez Cort?s
21 2241 fernando
 */
22 5005 jorpiell
public class TableFieldOperations extends Extension{
23 2241 fernando
24
    /**
25 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#initialize()
26 2241 fernando
     */
27 5005 jorpiell
    public void initialize() {
28 14821 jmvivo
            registerIcons();
29 2241 fernando
    }
30
31 14821 jmvivo
    private void registerIcons(){
32 15647 jmvivo
            PluginServices.getIconTheme().registerDefault(
33 14821 jmvivo
                                "table-order-asc",
34
                                this.getClass().getClassLoader().getResource("images/orderasc.png")
35
                        );
36
37 15647 jmvivo
            PluginServices.getIconTheme().registerDefault(
38 14821 jmvivo
                                "table-order-desc",
39
                                this.getClass().getClassLoader().getResource("images/orderdesc.png")
40
                        );
41
    }
42
43 2241 fernando
    /**
44 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
45 2241 fernando
     */
46
    public void execute(String actionCommand) {
47 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
48 2241 fernando
49
                if (v != null) {
50 6259 fjp
                        if (v instanceof Table) {
51 3940 caballero
52 4682 jorpiell
                            Table table = (Table) v;
53 6010 caballero
54 4682 jorpiell
                            doExecute(actionCommand,table);
55 9532 caballero
                            table.getModel().setModified(true);
56
                        }
57 2241 fernando
                }
58
    }
59 6010 caballero
60 4682 jorpiell
    /**
61
     * "execute" method acction
62
     * @param actionCommand
63
     * The acction command that executes this method
64
     * @param table
65
     * Table to operate
66
     */
67
        protected void doExecute(String actionCommand,Table table){
68
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
69 2241 fernando
70 4682 jorpiell
        DataSource sds=null;
71
                try {
72
                        sds = table.getModel().getModelo().getRecordset();
73 10626 caballero
                        String dsName = sds.getName();
74
75 6010 caballero
                    String fieldName=sds.getFieldName(fieldIndex);
76
            String sql = "select * from '" + dsName + "' order by " + fieldName;
77 4682 jorpiell
            if ("ORDERASC".equals(actionCommand)){
78
                sql += " asc;";
79
                    }else{
80
                sql += " desc;";
81
                    }
82 9833 fjp
                    DataSource ds = sds.getDataSourceFactory().executeSQL(sql, DataSourceFactory.MANUAL_OPENING);
83 4682 jorpiell
                    table.setOrder(ds.getWhereFilter());
84 10626 caballero
                } catch (ReadDriverException e) {
85 4682 jorpiell
            NotificationManager.addError("No se pudo ordenar", e);
86
        } catch (ParseException e) {
87
            throw new RuntimeException(e);
88
        } catch (DriverLoadException e) {
89
            NotificationManager.addError("Error con la carga de drivers", e);
90
        } catch (SemanticException e) {
91
            throw new RuntimeException(e);
92
        } catch (IOException e) {
93
            throw new RuntimeException(e);
94
        } catch (EvaluationException e) {
95
            throw new RuntimeException(e);
96
        }
97
        }
98
99 2241 fernando
    /**
100 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#isEnabled()
101 2241 fernando
     */
102
    public boolean isEnabled() {
103 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
104 2241 fernando
105
                if (v == null) {
106
                        return false;
107
                }
108
109 6127 fjp
                if (v instanceof Table) {
110 2241 fernando
                    Table t = (Table) v;
111
                    BitSet indices = t.getSelectedFieldIndices();
112
                    if (indices.cardinality() == 1){
113
                        return true;
114
                    }
115
                }
116
                return false;
117
    }
118
119
    /**
120 5005 jorpiell
     * @see com.iver.andami.plugins.IExtension#isVisible()
121 2241 fernando
     */
122
    public boolean isVisible() {
123 6880 cesar
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
124 2241 fernando
125
                if (v == null) {
126
                        return false;
127
                }
128
129 5900 jorpiell
                if (v instanceof Table) {
130 2241 fernando
                    return true;
131
                }
132
                return false;
133
    }
134
135
}