Statistics
| Revision:

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

History | View | Annotate | Download (3.97 KB)

1
package com.iver.cit.gvsig;
2

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

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

    
16

    
17
/**
18
 * DOCUMENT ME!
19
 *
20
 * @author Vicente Caballero Navarro
21
 */
22
public class TableRowsOperations extends Extension {
23
    /**
24
     * DOCUMENT ME!
25
     */
26
    public void initialize() {
27
    }
28

    
29
    /**
30
     * DOCUMENT ME!
31
     *
32
     * @param actionCommand DOCUMENT ME!
33
     */
34
    public void execute(String actionCommand) {
35
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
36

    
37
        if (v != null) {
38
            if (v.getClass() == Table.class) {
39
                Table table = (Table) v;
40
                if (actionCommand.compareTo("SELECTIONUP") == 0)
41
                        showsSelectedRows(table);
42
                if (actionCommand.compareTo("INVERTSELECTION") == 0)
43
                        invertSelection(table);
44
                table.getModel().setModified(true);
45
            }
46
        }
47
    }
48

    
49
    /**
50
     * Flip the selection (inverts selection)
51
     * @param table
52
     */
53
    private void invertSelection(Table table) {
54
            try {
55
                    SelectableDataSource sds = table.getModel().getModelo().getRecordset();
56
                    FBitSet selectedRows=sds.getSelection();
57
                    selectedRows.flip(0, (int)sds.getRowCount());
58
                    sds.setSelection(selectedRows);
59
            } catch (ReadDriverException e) {
60
                        e.printStackTrace();
61
                        NotificationManager.addError(e);
62
                }
63
    }
64

    
65
        private void showsSelectedRows(Table table) {
66
            long[] mapping=null;
67
                try {
68
                        mapping = new long[table.getModel().getModelo().getRowCount()];
69

    
70
                FBitSet selectedRows=table.getModel().getModelo().getSelection();
71
                int m=0;
72
                for (int i = selectedRows.nextSetBit(0); i >= 0;
73
                 i = selectedRows.nextSetBit(i + 1)) {
74
                        mapping[m]=i;
75
                        m++;
76
                }
77
                for (int i = 0; i < mapping.length;
78
                     i++) {
79
                        if (!selectedRows.get(i)) {
80
                                mapping[m]=i;
81
                                m++;
82
                        }
83
                }
84
                table.setOrder(mapping);
85
                } catch (ReadDriverException e) {
86
                        e.printStackTrace();
87
                }
88

    
89
        }
90
    /**
91
     * DOCUMENT ME!
92
     *
93
     * @return DOCUMENT ME!
94
     */
95
    public boolean isEnabled() {
96
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
97

    
98
        if (v == null) {
99
            return false;
100
        }
101

    
102
        if (v.getClass() == Table.class) {
103
            Table table = (Table) v;
104

    
105
            try {
106
                                return table.getModel().getModelo().getSelection().cardinality()>0;
107
                        } catch (ReadDriverException e) {
108
                                e.printStackTrace();
109
                        }
110
        }
111

    
112
        return false;
113
    }
114

    
115
    protected boolean doIsEnabled(Table table) {
116
                try {
117
                        BitSet indices = table.getSelectedFieldIndices();
118

    
119
                        System.out.println("TableNumericFieldOperations.isEnabled: Tabla: "
120
                                        + table.getModel().getModelo().getRecordset().getName());
121

    
122
                        if (indices.cardinality() == 1) {
123
                                int type = table.getModel().getModelo().getRecordset()
124
                                                .getFieldType(indices.nextSetBit(0));
125
                                if ((type == Types.BIGINT) || (type == Types.DECIMAL)
126
                                                || (type == Types.DOUBLE) || (type == Types.FLOAT)
127
                                                || (type == Types.INTEGER) || (type == Types.SMALLINT)
128
                                                || (type == Types.TINYINT) || (type == Types.REAL)
129
                                                || (type == Types.NUMERIC)) {
130
                                        return true;
131
                                }
132

    
133
                        }
134
                } catch (DriverLoadException e1) {
135
                        e1.printStackTrace();
136
                } catch (ReadDriverException e) {
137
                        e.printStackTrace();
138
                }
139

    
140
                return false;
141
        }
142

    
143
    /**
144
         * DOCUMENT ME!
145
         *
146
         * @return DOCUMENT ME!
147
         */
148
    public boolean isVisible() {
149
        IWindow v = PluginServices.getMDIManager().getActiveWindow();
150

    
151
        if (v == null) {
152
            return false;
153
        }
154

    
155
        if (v instanceof Table) {
156
            return true;
157
        }
158

    
159
        return false;
160
    }
161
}