Revision 45695

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/featuretable/SimpleFeaturesTableModelImpl.java
8 8
import javax.swing.JTable;
9 9
import javax.swing.table.AbstractTableModel;
10 10
import javax.swing.table.DefaultTableCellRenderer;
11
import org.apache.commons.lang3.StringUtils;
11 12
import org.gvsig.fmap.dal.complements.Search;
12 13
import org.gvsig.fmap.dal.exception.DataException;
13 14
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
......
222 223
    public boolean isCellEditable(int rowIndex, int columnIndex) {
223 224
        return false;
224 225
    }
226
    
227
    public FeatureAttributeDescriptor getFeatureDescriptor(int columnIndex) {
228
        String attrName = this.columnNames.get(columnIndex);
229
        if (this.featureType == null) {
230
            return null;
231
        }
232
        for (FeatureAttributeDescriptor attr : this.featureType.getAllAttributeDescriptors()) {
233
            if (StringUtils.equals(attrName,attr.getName())) {
234
                return attr;
235
            }
236
        }
237
        return null;
238
    }
225 239

  
226 240
    @Override
227 241
    public Feature get(int position) {
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.impl/src/main/java/org/gvsig/fmap/dal/swing/impl/searchpanel/DefaultSearchPanel.java
4 4
import java.awt.Cursor;
5 5
import java.awt.Dimension;
6 6
import java.awt.FlowLayout;
7
import java.awt.Toolkit;
8
import java.awt.datatransfer.Clipboard;
9
import java.awt.datatransfer.StringSelection;
10
import java.awt.datatransfer.Transferable;
7 11
import java.awt.event.ActionEvent;
8 12
import java.awt.event.ActionListener;
9 13
import java.awt.event.ComponentAdapter;
......
33 37
import javax.swing.JMenuItem;
34 38
import javax.swing.JOptionPane;
35 39
import javax.swing.JPopupMenu;
40
import javax.swing.JTable;
36 41
import javax.swing.ListSelectionModel;
37 42
import javax.swing.SwingUtilities;
43
import javax.swing.TransferHandler;
38 44
import javax.swing.event.ChangeEvent;
39 45
import javax.swing.event.ChangeListener;
40 46
import javax.swing.event.ListSelectionEvent;
......
313 319
            panel.doOrderBy();
314 320
        }
315 321
    }
322
    
323
    private class TablePopupMenu extends JPopupMenu {
324
            public final JTable table;
325
            public TablePopupMenu(JTable inputTable) {
326
                this.table = inputTable;
327
                JMenuItem copyRowsActionMenu = new JMenuItem("_Copy_rows");
328
                copyRowsActionMenu.addActionListener(new ActionListener() {
316 329

  
330
                    @Override
331
                    public void actionPerformed(ActionEvent e) {
332
                        doCopyRows(table);
333
                    }
334
                });
335
                this.add(copyRowsActionMenu);
336
            }
337
        }
338
            
317 339
    private class ActionButtons {
318 340

  
319 341
        private final DALActionFactory factory;
......
734 756
                    doShowCellInDialog();
735 757
                }
736 758
            }
737
        });
759
        });        
760
        
761
        this.tblResults.setComponentPopupMenu(new TablePopupMenu(this.tblResults));
762
        this.tblSearchPostProcessResults.setComponentPopupMenu(new TablePopupMenu(this.tblSearchPostProcessResults));
763
                
764
        //this.tblResults.add
738 765
        if (this.bookmarks.hasBookmark(this.store.getName())) {
739 766
            Bookmark<DefaultSearchParameters> initBookmark = this.bookmarks.get(this.store.getName());
740 767
            DefaultSearchParameters initSearchParams = initBookmark.getValue().getCopy();
......
1121 1148
        });
1122 1149
        dialog.show(WindowManager.MODE.DIALOG);
1123 1150
    }
1151
       
1152
    private void doCopyRows(JTable table) {
1153
        
1154
        SimpleFeaturesTableModel model = null;
1155
        if (table.getModel() instanceof SimpleFeaturesTableModel) {
1156
            model = (SimpleFeaturesTableModel) table.getModel();
1157
        }
1124 1158

  
1159
        String LINE_BREAK = System.lineSeparator();
1160
        String CELL_BREAK = "\t";
1161
        char DELIMITER = '"';
1162
        
1163
        Clipboard CLIPBOARD = Toolkit.getDefaultToolkit().getSystemClipboard();
1164
        
1165
        int[] selection = table.getSelectedRows();
1166
        for (int i = 0; i < selection.length; i++) {
1167
          selection[i] = table.convertRowIndexToModel(selection[i]);
1168
        }
1169
   
1170
        int numCols = table.getColumnCount();
1171
        StringBuilder excelStr = new StringBuilder();
1172
        boolean valueIsNumeric;
1173

  
1174
        for (int j = 0; j < numCols; j++) {
1175

  
1176
            excelStr.append(DELIMITER);
1177
            excelStr.append(escape(table.getColumnName(j), LINE_BREAK, CELL_BREAK));
1178
            excelStr.append(DELIMITER);
1179

  
1180
            if (j < numCols - 1) {
1181
                excelStr.append(CELL_BREAK);
1182
            }
1183
        }
1184
        excelStr.append(LINE_BREAK);
1185
        for (int i : selection) {
1186
            for (int j = 0; j < numCols; j++) {
1187
                valueIsNumeric = false;
1188
                if (model != null) {
1189
                    FeatureAttributeDescriptor descriptor = model.getFeatureDescriptor(j);
1190
                    if (descriptor.getDataType().isNumeric()) {
1191
                        if (!descriptor.hasAvailableValues()) {
1192
                            valueIsNumeric = true;
1193
                        }
1194
                    }
1195
                }
1196
                if (!valueIsNumeric) {
1197
                    excelStr.append(DELIMITER);
1198
                }
1199

  
1200
                excelStr.append(escape(table.getValueAt(i, j), LINE_BREAK, CELL_BREAK));
1201

  
1202
                if (!valueIsNumeric) {
1203
                    excelStr.append(DELIMITER);
1204
                }
1205
                if (j < numCols - 1) {
1206
                    excelStr.append(CELL_BREAK);
1207
                }
1208
            }
1209
            excelStr.append(LINE_BREAK);
1210
        }
1211

  
1212
        StringSelection sel = new StringSelection(excelStr.toString());
1213
        CLIPBOARD.setContents(sel, sel);
1214
    }
1215

  
1216
    private String escape(Object cell,String LINE_BREAK, String CELL_BREAK) {
1217
        return (cell == null ? "" : cell.toString()
1218
                .replace(CELL_BREAK, " ")
1219
                );
1220
    }
1221
       
1125 1222
    @Override
1126 1223
    public ImageIcon loadImage(String imageName) {
1127 1224
        String name = FilenameUtils.getBaseName(imageName);
......
1598 1695
    }
1599 1696

  
1600 1697
}
1601
  
1698
  
trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.dal/org.gvsig.fmap.dal.swing/org.gvsig.fmap.dal.swing.api/src/main/java/org/gvsig/fmap/dal/swing/featuretable/SimpleFeaturesTableModel.java
26 26
import javax.swing.JTable;
27 27
import javax.swing.table.TableModel;
28 28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
29 30
import org.gvsig.tools.dispose.Disposable;
30 31
import org.gvsig.tools.util.UnmodifiableBasicList;
31 32

  
......
41 42

  
42 43
    public void setCellRenderers(JTable table);
43 44
    
45
    public FeatureAttributeDescriptor getFeatureDescriptor(int columnIndex);
46
    
44 47
}

Also available in: Unified diff