Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableFieldOperations.java @ 9532

History | View | Annotate | Download (3.46 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.io.IOException;
4
import java.util.BitSet;
5

    
6
import com.hardcode.driverManager.DriverLoadException;
7
import com.hardcode.gdbms.engine.data.DataSource;
8
import com.hardcode.gdbms.engine.data.DataSourceFactory;
9
import com.hardcode.gdbms.engine.data.driver.DriverException;
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
import com.iver.andami.ui.mdiManager.IWindow;
17
import com.iver.cit.gvsig.project.documents.table.gui.Table;
18

    
19
/**
20
 * @author Fernando Gonz?lez Cort?s
21
 */
22
public class TableFieldOperations extends Extension{
23

    
24
    /**
25
     * @see com.iver.andami.plugins.IExtension#initialize()
26
     */
27
    public void initialize() {
28
    }
29

    
30
    /**
31
     * @see com.iver.andami.plugins.IExtension#execute(java.lang.String)
32
     */
33
    public void execute(String actionCommand) {
34
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
35

    
36
                if (v != null) {
37
                        if (v instanceof Table) {
38

    
39
                            Table table = (Table) v;
40

    
41
                            doExecute(actionCommand,table);
42
                            table.getModel().setModified(true);
43
                        }
44
                }
45
    }
46

    
47
    /**
48
     * "execute" method acction
49
     * @param actionCommand
50
     * The acction command that executes this method
51
     * @param table
52
     * Table to operate
53
     */
54
        protected void doExecute(String actionCommand,Table table){
55
                int fieldIndex = table.getSelectedFieldIndices().nextSetBit(0);
56

    
57
        DataSource sds=null;
58
                try {
59
                        sds = table.getModel().getModelo().getRecordset();
60
                } catch (DriverLoadException e1) {
61
                        // TODO Auto-generated catch block
62
                        e1.printStackTrace();
63
                }
64
            String dsName = sds.getName();
65
            try {
66
                    String fieldName=sds.getFieldName(fieldIndex);
67
            String sql = "select * from '" + dsName + "' order by " + fieldName;
68
            if ("ORDERASC".equals(actionCommand)){
69
                sql += " asc;";
70
                    }else{
71
                sql += " desc;";
72
                    }
73
                    DataSource ds = sds.getDataSourceFactory().executeSQL(sql, DataSourceFactory.AUTOMATIC_OPENING);
74
                    table.setOrder(ds.getWhereFilter());
75
                } catch (DriverException e) {
76
            NotificationManager.addError("No se pudo ordenar", e);
77
        } catch (ParseException e) {
78
            throw new RuntimeException(e);
79
        } catch (DriverLoadException e) {
80
            NotificationManager.addError("Error con la carga de drivers", e);
81
        } catch (SemanticException e) {
82
            throw new RuntimeException(e);
83
        } catch (IOException e) {
84
            throw new RuntimeException(e);
85
        } catch (EvaluationException e) {
86
            throw new RuntimeException(e);
87
        }
88
        }
89

    
90
    /**
91
     * @see com.iver.andami.plugins.IExtension#isEnabled()
92
     */
93
    public boolean isEnabled() {
94
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
95

    
96
                if (v == null) {
97
                        return false;
98
                }
99

    
100
                if (v instanceof Table) {
101
                    Table t = (Table) v;
102
                    BitSet indices = t.getSelectedFieldIndices();
103
                    if (indices.cardinality() == 1){
104
                        return true;
105
                    }
106
                }
107
                return false;
108
    }
109

    
110
    /**
111
     * @see com.iver.andami.plugins.IExtension#isVisible()
112
     */
113
    public boolean isVisible() {
114
                IWindow v = PluginServices.getMDIManager().getActiveWindow();
115

    
116
                if (v == null) {
117
                        return false;
118
                }
119

    
120
                if (v instanceof Table) {
121
                    return true;
122
                }
123
                return false;
124
    }
125

    
126
}