List of usage examples for javax.swing DefaultListModel add
public void add(int index, E element)
From source file:org.rimudb.editor.DescriptorEditor.java
/** * Build the panel//from www . j a v a 2 s . c o m */ private JPanel createColumnTablePanel() { JPanel columnPanel = new JPanel(); columnPanel.setLayout(new BoxLayout(columnPanel, BoxLayout.Y_AXIS)); // Create the property table panel propertyModel = new PropertyTableModel(); // Add a listener to set the changed state propertyModel.addTableModelListener(new TableModelListener() { public void tableChanged(TableModelEvent e) { markChanged(); if (e instanceof PropertyTableModelEvent) { PropertyTableModelEvent ptme = (PropertyTableModelEvent) e; // If the columnName column was changed then check it isn't // a PK if (ptme.getColumn() == 1) { String beforeColumnName = (String) ptme.getBeforeValue(); String afterColumnName = (String) ptme.getAfterValue(); // Is the field entry in the list of primary keys? for (int i = 0; i < pkListModel.getSize(); i++) { String pkColumnName = (String) pkListModel.get(i); // If it's found then remove it if (beforeColumnName.equals(pkColumnName)) { pkListModel.set(i, afterColumnName); break; } } } } } }); table = new ATable(getPropertyModel()); table.setName("ColumnTable"); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); table.getSelectionModel().addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int selectedRowCount = table.getSelectedRowCount(); removeColumnBtn.setEnabled(selectedRowCount > 0); moveUpBtn.setEnabled(selectedRowCount > 0); moveDownBtn.setEnabled(selectedRowCount > 0); } }); table.setTransferHandler(new TransferHandler() { public int getSourceActions(JComponent c) { return COPY; } protected Transferable createTransferable(JComponent c) { ATable columnTable = (ATable) c; int row = columnTable.getSelectedRow(); String columnName = getPropertyModel().getRow(row).getColumnName(); return new StringSelection(columnName); } }); table.setDragEnabled(true); JScrollPane sp = new JScrollPane(table); sp.setMaximumSize(new Dimension(Short.MAX_VALUE, 325)); sp.setPreferredSize(new Dimension(Short.MAX_VALUE, 325)); sp.setMinimumSize(new Dimension(Short.MAX_VALUE, 325)); JComboBox typeCB = new JComboBox(DatabaseTypes.getAllTypes()); typeCB.setEditable(false); javax.swing.table.TableColumn typeColumn = table.getColumnModel().getColumn(2); typeColumn.setCellEditor(new DefaultCellEditor(typeCB)); // Create the popup menu and set it on the table propertyPopup = new TablePopupMenu(this, table); table.addMouseListener(propertyPopup); sp.addMouseListener(propertyPopup); sp.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(sp); columnPanel.add(Box.createVerticalStrut(10)); JLabel pkLabel = new JLabel("Primary Key Columns", SwingConstants.LEFT); pkLabel.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(pkLabel); pkListModel = new DefaultListModel(); pkListModel.addListDataListener(new ListDataListener() { public void intervalRemoved(ListDataEvent e) { markChanged(); } public void intervalAdded(ListDataEvent e) { markChanged(); } public void contentsChanged(ListDataEvent e) { markChanged(); } }); pkList = new JList(pkListModel); pkList.setName("pkList"); pkList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); pkList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int selectedRowCount = pkList.getSelectedIndex(); removePkBtn.setEnabled(selectedRowCount > -1); } }); pkList.setTransferHandler(new TransferHandler() { public boolean canImport(TransferHandler.TransferSupport info) { // we only import Strings if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); if (dl.getIndex() == -1) { return false; } return true; } public boolean importData(TransferHandler.TransferSupport info) { if (!info.isDrop()) { return false; } // Check for String flavor if (!info.isDataFlavorSupported(DataFlavor.stringFlavor)) { displayDropLocation("List doesn't accept a drop of this type."); return false; } JList.DropLocation dl = (JList.DropLocation) info.getDropLocation(); DefaultListModel listModel = (DefaultListModel) pkList.getModel(); int index = dl.getIndex(); // Get the string that is being dropped. Transferable t = info.getTransferable(); String data; try { data = (String) t.getTransferData(DataFlavor.stringFlavor); } catch (Exception e) { return false; } // If this is a copy action then check we don't already have that String if (info.getDropAction() == COPY && listModel.indexOf(data) > -1) { displayDropLocation("The column " + data + " is already a primary key"); return false; } // Perform the actual import. if (dl.isInsert()) { int oldIndex = listModel.indexOf(data); if (oldIndex < index) { listModel.add(index, data); listModel.remove(oldIndex); } else { listModel.remove(oldIndex); listModel.add(index, data); } } else { // Don't handle replacements } return true; } public int getSourceActions(JComponent c) { return MOVE; } protected Transferable createTransferable(JComponent c) { JList list = (JList) c; Object[] values = list.getSelectedValues(); StringBuffer buff = new StringBuffer(); for (int i = 0; i < values.length; i++) { Object val = values[i]; buff.append(val == null ? "" : val.toString()); if (i != values.length - 1) { buff.append("\n"); } } return new StringSelection(buff.toString()); } }); pkList.setDropMode(DropMode.INSERT); pkList.setDragEnabled(true); JScrollPane pkScrollPanel = new JScrollPane(pkList); pkScrollPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, 100)); pkScrollPanel.setAlignmentX(LEFT_ALIGNMENT); columnPanel.add(pkScrollPanel); return columnPanel; }
From source file:tvbrowser.core.search.AbstractSearcher.java
public synchronized Program[] search(ProgramFieldType[] fieldArr, Date startDate, int nrDays, Channel[] channels, boolean sortByStartTime, ProgressMonitor progress, final DefaultListModel listModel) { // Should we search in all channels? if (channels == null) { channels = Settings.propSubscribedChannels.getChannelArray(); }//from ww w . j a v a 2s. c o m if (nrDays < 0) { // Search complete data, beginning yesterday to 4 weeks into the future startDate = Date.getCurrentDate().addDays(-1); nrDays = TvDataBase.getInstance().getMaxSupportedDate().getNumberOfDaysSince(startDate); } // Perform the actual search ArrayList<Program> hitList = new ArrayList<Program>(); int lastDayWithData = 0; if (progress != null) { progress.setMaximum(channels.length * (nrDays + 1)); } for (int day = 0; day <= nrDays; day++) { for (int channelIdx = 0; channelIdx < channels.length; channelIdx++) { if (progress != null) { progress.setValue(day * channels.length + channelIdx); } Channel channel = channels[channelIdx]; if (channel != null) { ChannelDayProgram dayProg = TvDataBase.getInstance().getDayProgram(startDate, channel); if (dayProg != null) { // This day has data -> remember it lastDayWithData = day; // Search this day program for (int i = 0; i < dayProg.getProgramCount(); i++) { final Program prog = dayProg.getProgramAt(i); if (matches(prog, fieldArr)) { if (listModel != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { int insertIndex = 0; for (int index = 0; index < listModel.getSize(); index++) { Program p = (Program) listModel.get(index); if (ProgramUtilities.getProgramComparator().compare(p, prog) < 0) { insertIndex = index + 1; } } listModel.add(insertIndex, prog); } }); } hitList.add(prog); } } } } } // Give up if we did not find data for the last 10 days if ((day - lastDayWithData) > 10) { break; } // The next day startDate = startDate.addDays(1); } // Convert the list into an array Program[] hitArr = new Program[hitList.size()]; hitList.toArray(hitArr); // Sort the array if wanted if (sortByStartTime) { Arrays.sort(hitArr, getStartTimeComparator()); } if (progress != null) { progress.setValue(0); progress.setMessage(""); } // return the result return hitArr; }
From source file:util.ui.UiUtilities.java
/** * Moves Selected Items from one List to another * * @param fromList/*from w ww. j a va 2 s.c o m*/ * Move from this List * @param toList * Move into this List * @return Moved Elements */ public static Object[] moveSelectedItems(JList fromList, JList toList) { DefaultListModel fromModel = (DefaultListModel) fromList.getModel(); DefaultListModel toModel = (DefaultListModel) toList.getModel(); // get the selection int[] selection = fromList.getSelectedIndices(); if (selection.length == 0) { return new Object[] {}; } Object[] objects = new Object[selection.length]; for (int i = 0; i < selection.length; i++) { objects[i] = fromModel.getElementAt(selection[i]); } // get the target insertion position int targetPos = toList.getMaxSelectionIndex(); if (targetPos == -1) { targetPos = toModel.getSize(); } else { targetPos++; } // suppress updates on both lists if (selection.length >= 5) { fromList.setModel(new DefaultListModel()); toList.setModel(new DefaultListModel()); } // move the elements for (int i = selection.length - 1; i >= 0; i--) { Object value = fromModel.remove(selection[i]); toModel.add(targetPos, value); } if (selection.length >= 5) { fromList.setModel(fromModel); toList.setModel(toModel); } // change selection of the fromList if (fromModel.getSize() > 0) { int newSelection = selection[0]; if (newSelection >= fromModel.getSize()) { newSelection = fromModel.getSize() - 1; } fromList.setSelectedIndex(newSelection); } if (selection.length >= 5) { fromList.repaint(); fromList.revalidate(); toList.repaint(); toList.revalidate(); } // change selection of the toList toList.setSelectionInterval(targetPos, targetPos + selection.length - 1); // ensure the selection is visible toList.ensureIndexIsVisible(toList.getMaxSelectionIndex()); toList.ensureIndexIsVisible(toList.getMinSelectionIndex()); return objects; }
From source file:util.ui.UiUtilities.java
/** * Move selected Items in the JList//from ww w. j av a 2 s . c o m * * @param list * Move Items in this List * @param nrRows * Move Items nrRows up/down */ public static void moveSelectedItems(JList list, int nrRows) { DefaultListModel model = (DefaultListModel) list.getModel(); // get the selection int[] selection = list.getSelectedIndices(); if (selection.length == 0) { return; } // Remove the selected items Object[] items = new Object[selection.length]; for (int i = selection.length - 1; i >= 0; i--) { items[i] = model.remove(selection[i]); } // insert the elements at the target position int targetPos = selection[0] + nrRows; targetPos = Math.max(targetPos, 0); targetPos = Math.min(targetPos, model.getSize()); for (int i = 0; i < items.length; i++) { model.add(targetPos + i, items[i]); } // change selection of the toList list.setSelectionInterval(targetPos, targetPos + selection.length - 1); // ensure the selection is visible list.ensureIndexIsVisible(list.getMaxSelectionIndex()); list.ensureIndexIsVisible(list.getMinSelectionIndex()); }