Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / TableRowsOperations.java @ 37941

History | View | Annotate | Download (4.23 KB)

1
package com.iver.cit.gvsig;
2

    
3
import java.sql.Types;
4
import java.util.BitSet;
5

    
6
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7
import com.iver.andami.PluginServices;
8
import com.iver.andami.messages.NotificationManager;
9
import com.iver.andami.plugins.Extension;
10
import com.iver.andami.ui.mdiManager.IWindow;
11
import com.iver.cit.gvsig.fmap.layers.FBitSet;
12
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
13
import com.iver.cit.gvsig.project.documents.table.gui.Table;
14

    
15

    
16
/**
17
 * DOCUMENT ME!
18
 *
19
 * @author Vicente Caballero Navarro
20
 */
21
public class TableRowsOperations extends Extension {
22
    /**
23
     * DOCUMENT ME!
24
     */
25
    public void initialize() {
26
            registerIcons();
27
    }
28
    
29
    private void registerIcons(){
30
            PluginServices.getIconTheme().registerDefault(
31
                                "table-selection-up",
32
                                this.getClass().getClassLoader().getResource("images/selectionUp.png")
33
                        );
34
    
35
            PluginServices.getIconTheme().registerDefault(
36
                                "table-invert",
37
                                this.getClass().getClassLoader().getResource("images/invertSelection.png")
38
                        );
39
    }
40

    
41
    /**
42
     * DOCUMENT ME!
43
     *
44
     * @param actionCommand DOCUMENT ME!
45
     */
46
    public void execute(String actionCommand) {
47
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
48

    
49
        if (v != null) {
50
            if (v.getClass() == Table.class) {
51
                Table table = (Table) v;
52
                if (actionCommand.compareTo("SELECTIONUP") == 0)
53
                        showsSelectedRows(table);
54
                if (actionCommand.compareTo("INVERTSELECTION") == 0)
55
                        invertSelection(table);
56
                table.getModel().setModified(true);
57
            }
58
        }
59
    }
60

    
61
    /**
62
     * Flip the selection (inverts selection)
63
     * @param table
64
     */
65
    private void invertSelection(Table table) {
66
            try {
67
                    SelectableDataSource sds = table.getModel().getModelo().getRecordset();
68
                    FBitSet selectedRows=sds.getSelection();
69
                    selectedRows.flip(0, (int)sds.getRowCount());
70
                    sds.setSelection(selectedRows);
71
            } catch (ReadDriverException e) {
72
                        e.printStackTrace();
73
                        NotificationManager.addError(e);
74
                }
75
    }
76

    
77
        private void showsSelectedRows(Table table) {
78
            long[] mapping=null;
79
                try {
80
                        mapping = new long[table.getModel().getModelo().getRowCount()];
81

    
82
                FBitSet selectedRows=table.getModel().getModelo().getSelection();
83
                int m=0;
84
                for (int i = selectedRows.nextSetBit(0); i >= 0;
85
                 i = selectedRows.nextSetBit(i + 1)) {
86
                        mapping[m]=i;
87
                        m++;
88
                }
89
                for (int i = 0; i < mapping.length;
90
                     i++) {
91
                        if (!selectedRows.get(i)) {
92
                                mapping[m]=i;
93
                                m++;
94
                        }
95
                }
96
                table.setOrder(mapping);
97
                } catch (ReadDriverException e) {
98
                        e.printStackTrace();
99
                }
100

    
101
        }
102
    /**
103
     * DOCUMENT ME!
104
     *
105
     * @return DOCUMENT ME!
106
     */
107
    public boolean isEnabled() {
108
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
109

    
110
        if (v == null) {
111
            return false;
112
        }
113

    
114
        if (v.getClass() == Table.class) {
115
            Table table = (Table) v;
116

    
117
            try {
118
                                return table.getModel().getModelo().getSelection().cardinality()>0;
119
                        } catch (ReadDriverException e) {
120
                                e.printStackTrace();
121
                        }
122
        }
123

    
124
        return false;
125
    }
126

    
127
    protected boolean doIsEnabled(Table table) {
128
                try {
129
                        BitSet indices = table.getSelectedFieldIndices();
130

    
131
                        System.out.println("TableNumericFieldOperations.isEnabled: Tabla: "
132
                                        + table.getModel().getModelo().getRecordset().getName());
133

    
134
                        if (indices.cardinality() == 1) {
135
                                int type = table.getModel().getModelo().getRecordset()
136
                                                .getFieldType(indices.nextSetBit(0));
137
                                if ((type == Types.BIGINT) || (type == Types.DECIMAL)
138
                                                || (type == Types.DOUBLE) || (type == Types.FLOAT)
139
                                                || (type == Types.INTEGER) || (type == Types.SMALLINT)
140
                                                || (type == Types.TINYINT) || (type == Types.REAL)
141
                                                || (type == Types.NUMERIC)) {
142
                                        return true;
143
                                }
144

    
145
                        }
146
                } catch (ReadDriverException e) {
147
                        e.printStackTrace();
148
                }
149

    
150
                return false;
151
        }
152

    
153
    /**
154
         * DOCUMENT ME!
155
         *
156
         * @return DOCUMENT ME!
157
         */
158
    public boolean isVisible() {
159
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
160

    
161
        if (v == null) {
162
            return false;
163
        }
164

    
165
        if (v instanceof Table) {
166
            return true;
167
        }
168

    
169
        return false;
170
    }
171
}