Statistics
| Revision:

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

History | View | Annotate | Download (4.23 KB)

1 6238 caballero
package com.iver.cit.gvsig;
2
3 7738 jaume
import java.sql.Types;
4
import java.util.BitSet;
5 6238 caballero
6 10626 caballero
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
7 6238 caballero
import com.iver.andami.PluginServices;
8
import com.iver.andami.messages.NotificationManager;
9
import com.iver.andami.plugins.Extension;
10 6877 cesar
import com.iver.andami.ui.mdiManager.IWindow;
11 6238 caballero
import com.iver.cit.gvsig.fmap.layers.FBitSet;
12
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
13 7304 caballero
import com.iver.cit.gvsig.project.documents.table.gui.Table;
14 6238 caballero
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 14821 jmvivo
            registerIcons();
27 6238 caballero
    }
28 14821 jmvivo
29
    private void registerIcons(){
30 15640 jmvivo
            PluginServices.getIconTheme().registerDefault(
31 14821 jmvivo
                                "table-selection-up",
32
                                this.getClass().getClassLoader().getResource("images/selectionUp.png")
33
                        );
34
35 15640 jmvivo
            PluginServices.getIconTheme().registerDefault(
36 14821 jmvivo
                                "table-invert",
37
                                this.getClass().getClassLoader().getResource("images/invertSelection.png")
38
                        );
39
    }
40 6238 caballero
41
    /**
42
     * DOCUMENT ME!
43
     *
44
     * @param actionCommand DOCUMENT ME!
45
     */
46
    public void execute(String actionCommand) {
47 6880 cesar
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
48 6238 caballero
49
        if (v != null) {
50
            if (v.getClass() == Table.class) {
51
                Table table = (Table) v;
52 7163 fjp
                if (actionCommand.compareTo("SELECTIONUP") == 0)
53
                        showsSelectedRows(table);
54
                if (actionCommand.compareTo("INVERTSELECTION") == 0)
55
                        invertSelection(table);
56 9532 caballero
                table.getModel().setModified(true);
57 6238 caballero
            }
58
        }
59
    }
60
61 7163 fjp
    /**
62
     * Flip the selection (inverts selection)
63
     * @param table
64
     */
65
    private void invertSelection(Table table) {
66 7572 caballero
            try {
67 10626 caballero
                    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 7572 caballero
                        e.printStackTrace();
73
                        NotificationManager.addError(e);
74
                }
75 10626 caballero
    }
76 7163 fjp
77
        private void showsSelectedRows(Table table) {
78 6238 caballero
            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 10626 caballero
                } catch (ReadDriverException e) {
98 6238 caballero
                        e.printStackTrace();
99
                }
100
101
        }
102
    /**
103
     * DOCUMENT ME!
104
     *
105
     * @return DOCUMENT ME!
106
     */
107
    public boolean isEnabled() {
108 6880 cesar
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
109 6238 caballero
110
        if (v == null) {
111
            return false;
112
        }
113
114
        if (v.getClass() == Table.class) {
115
            Table table = (Table) v;
116
117 10626 caballero
            try {
118
                                return table.getModel().getModelo().getSelection().cardinality()>0;
119
                        } catch (ReadDriverException e) {
120
                                e.printStackTrace();
121
                        }
122 6238 caballero
        }
123
124
        return false;
125
    }
126
127
    protected boolean doIsEnabled(Table table) {
128 10626 caballero
                try {
129
                        BitSet indices = table.getSelectedFieldIndices();
130 6238 caballero
131 10626 caballero
                        System.out.println("TableNumericFieldOperations.isEnabled: Tabla: "
132
                                        + table.getModel().getModelo().getRecordset().getName());
133 6238 caballero
134 10626 caballero
                        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 6238 caballero
145 10626 caballero
                        }
146
                } catch (ReadDriverException e) {
147
                        e.printStackTrace();
148
                }
149 6238 caballero
150 10626 caballero
                return false;
151
        }
152 6238 caballero
153
    /**
154 10626 caballero
         * DOCUMENT ME!
155
         *
156
         * @return DOCUMENT ME!
157
         */
158 6238 caballero
    public boolean isVisible() {
159 6880 cesar
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
160 6238 caballero
161
        if (v == null) {
162
            return false;
163
        }
164
165
        if (v instanceof Table) {
166
            return true;
167
        }
168
169
        return false;
170
    }
171
}