Statistics
| Revision:

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

History | View | Annotate | Download (4.06 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
                        return true;
116
        }
117

    
118
        return false;
119
    }
120

    
121
    protected boolean doIsEnabled(Table table) {
122
                try {
123
                        BitSet indices = table.getSelectedFieldIndices();
124

    
125
                        System.out.println("TableNumericFieldOperations.isEnabled: Tabla: "
126
                                        + table.getModel().getModelo().getRecordset().getName());
127

    
128
                        if (indices.cardinality() == 1) {
129
                                int type = table.getModel().getModelo().getRecordset()
130
                                                .getFieldType(indices.nextSetBit(0));
131
                                if ((type == Types.BIGINT) || (type == Types.DECIMAL)
132
                                                || (type == Types.DOUBLE) || (type == Types.FLOAT)
133
                                                || (type == Types.INTEGER) || (type == Types.SMALLINT)
134
                                                || (type == Types.TINYINT) || (type == Types.REAL)
135
                                                || (type == Types.NUMERIC)) {
136
                                        return true;
137
                                }
138

    
139
                        }
140
                } catch (ReadDriverException e) {
141
                        e.printStackTrace();
142
                }
143

    
144
                return false;
145
        }
146

    
147
    /**
148
         * DOCUMENT ME!
149
         *
150
         * @return DOCUMENT ME!
151
         */
152
    public boolean isVisible() {
153
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
154

    
155
        if (v == null) {
156
            return false;
157
        }
158

    
159
        if (v instanceof Table) {
160
            return true;
161
        }
162

    
163
        return false;
164
    }
165
}