List of usage examples for javax.swing DefaultListModel removeElement
public boolean removeElement(Object obj)
From source file:edu.ku.brc.specify.tasks.subpane.VisualQueryPanel.java
/** * Moves selected items from one list to the other. * @param srcList//w ww.jav a 2 s .c o m * @param srcHash * @param dstList * @param dstHash */ private void moveItems(final JList srcList, final HashSet<Integer> srcHash, final JList dstList, final HashSet<Integer> dstHash) { int inx = srcList.getSelectedIndex(); if (inx > -1) { DefaultListModel srcModel = (DefaultListModel) srcList.getModel(); DefaultListModel dstModel = (DefaultListModel) dstList.getModel(); int[] indexes = srcList.getSelectedIndices(); ArrayList<LatLonPoint> llpList = new ArrayList<LatLonPoint>(indexes.length); for (int selInx : indexes) { LatLonPoint llp = (LatLonPoint) srcModel.get(selInx); llpList.add(llp); if (!dstHash.contains(llp.getLocId())) { dstModel.addElement(llp); dstHash.add(llp.getLocId()); } } for (LatLonPoint llp : llpList) { srcModel.removeElement(llp); srcHash.remove(llp.getLocId()); } } }
From source file:GUI.MainWindow.java
public void deleteReferences(JTree tree, JList list) { DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent(); if (node == null) { return;//from w w w . j a va 2 s . c om } Object obj = node.getUserObject(); if (!(obj instanceof Vulnerability)) { return; } // Get currently selected vulnerability Vulnerability vuln = (Vulnerability) obj; DefaultListModel dlm = (DefaultListModel) list.getModel(); List selected = list.getSelectedValuesList(); for (Object ref_obj : selected) { if (ref_obj instanceof Reference) { Reference ref = (Reference) ref_obj; vuln.deleteReference(ref); dlm.removeElement(ref_obj); System.out.println("Deleted Reference: " + ref); } else { System.out.println("Somehow the references list contained a non-Regerence object"); } } }
From source file:eu.crisis_economics.abm.dashboard.Page_Parameters.java
private void addParameterToTree(final AvailableParameter[] parameters, final ParameterCombinationGUI pcGUI) { final DefaultListModel listModel = (DefaultListModel) parameterList.getModel(); final DefaultTreeModel treeModel = (DefaultTreeModel) pcGUI.tree.getModel(); final DefaultMutableTreeNode root = (DefaultMutableTreeNode) treeModel.getRoot(); for (final AvailableParameter parameter : parameters) { listModel.removeElement(parameter); final ParameterInfo selectedInfo = parameter.info; final DefaultMutableTreeNode newNode = new DefaultMutableTreeNode( new ParameterInATree(selectedInfo, currentModelHandler.getModelClass())); treeModel.insertNodeInto(newNode, root, root.getChildCount()); if (selectedInfo instanceof SubmodelInfo) { final SubmodelInfo sInfo = (SubmodelInfo) selectedInfo; if (sInfo.getActualType() != null) { addSubParametersToTree(sInfo, pcGUI.tree, newNode); pcGUI.tree.expandPath(new TreePath(treeModel.getPathToRoot(newNode))); }//from w w w. j a v a 2s. com } } updateNumberOfRuns(); pcGUI.tree.expandPath(new TreePath(treeModel.getPathToRoot(root))); }
From source file:GUI.MainWindow.java
public void addReference(JTree tree, JList list, Reference current) { DefaultMutableTreeNode node = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent()); if (node == null) { return;/*from w w w . j a v a2s . co m*/ } Object obj = node.getUserObject(); if (!(obj instanceof Vulnerability)) { return; // here be monsters, most likely in the merge tree } Vulnerability vuln = (Vulnerability) obj; DefaultListModel dlm = (DefaultListModel) list.getModel(); // Build Input Field and display it JTextField description = new JTextField(); JTextField url = new JTextField(); // If current is not null then pre-set the description and risk if (current != null) { description.setText(current.getDescription()); url.setText(current.getUrl()); } JLabel error = new JLabel( "A valid URL needs to be supplied including the protocol i.e. http://www.github.com"); error.setForeground(Color.red); Object[] message = { "Description:", description, "URL:", url }; String url_string = null; Reference newref = null; while (url_string == null) { int option = JOptionPane.showConfirmDialog(null, message, "Add Reference", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { System.out.println("User clicked ok, validating data"); String ref_desc = description.getText(); String ref_url = url.getText(); if (!ref_desc.equals("") || !ref_url.equals("")) { // Both have values // Try to validate URL try { URL u = new URL(url.getText()); u.toURI(); url_string = url.getText(); // Causes loop to end with a valid url } catch (MalformedURLException ex) { url_string = null; //ex.printStackTrace(); } catch (URISyntaxException ex) { url_string = null; //ex.printStackTrace(); } } } else if (option == JOptionPane.CANCEL_OPTION || option == JOptionPane.CLOSED_OPTION) { System.out.println("User clicked cancel/close"); return; // ends the loop without making any chages } if (url_string == null) { // We need to show an error saying that the url failed to parse Object[] message2 = { error, "Description:", description, "URL:", url }; message = message2; } } // If you get here there is a valid reference URL and description Reference ref = new Reference(description.getText(), url.getText()); if (current == null) { // Add it to the vuln vuln.addReference(ref); // Add it to the GUI dlm.addElement(ref); System.out.println("Valid reference added: " + ref); } else { // Modify it in the vuln vuln.modifyReference(current, ref); // Update the GUI dlm.removeElement(current); dlm.addElement(ref); System.out.println("Valid reference modified: " + ref); } }
From source file:gtu._work.ui.RegexDirReplacer.java
private void initGUI() { try {/*w w w . j av a 2 s .com*/ { } BorderLayout thisLayout = new BorderLayout(); getContentPane().setLayout(thisLayout); this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); { jTabbedPane1 = new JTabbedPane(); getContentPane().add(jTabbedPane1, BorderLayout.CENTER); { jPanel1 = new JPanel(); BorderLayout jPanel1Layout = new BorderLayout(); jPanel1.setLayout(jPanel1Layout); jTabbedPane1.addTab("source", null, jPanel1, null); { jScrollPane1 = new JScrollPane(); jPanel1.add(jScrollPane1, BorderLayout.CENTER); { ListModel srcListModel = new DefaultListModel(); srcList = new JList(); jScrollPane1.setViewportView(srcList); srcList.setModel(srcListModel); { panel = new JPanel(); jScrollPane1.setRowHeaderView(panel); panel.setLayout(new FormLayout( new ColumnSpec[] { ColumnSpec.decode("default:grow"), }, new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, FormFactory.RELATED_GAP_ROWSPEC, FormFactory.DEFAULT_ROWSPEC, })); { childrenDirChkbox = new JCheckBox("??"); childrenDirChkbox.setSelected(true); panel.add(childrenDirChkbox, "1, 1"); } { subFileNameText = new JTextField(); panel.add(subFileNameText, "1, 2, fill, default"); subFileNameText.setColumns(10); subFileNameText.setText("(txt|java)"); } { replaceOldFileChkbox = new JCheckBox(""); replaceOldFileChkbox.setSelected(true); panel.add(replaceOldFileChkbox, "1, 3"); } { charsetText = new JTextField(); panel.add(charsetText, "1, 5, fill, default"); charsetText.setColumns(10); charsetText.setText("UTF8"); } } srcList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { JListUtil.newInstance(srcList).defaultMouseClickOpenFile(evt); JPopupMenuUtil.newInstance(srcList).applyEvent(evt)// .addJMenuItem("load files from clipboard", new ActionListener() { public void actionPerformed(ActionEvent arg0) { String content = ClipboardUtil.getInstance().getContents(); DefaultListModel model = (DefaultListModel) srcList.getModel(); StringTokenizer tok = new StringTokenizer(content, "\t\n\r\f", false); for (; tok.hasMoreElements();) { String val = ((String) tok.nextElement()).trim(); model.addElement(new File(val)); } } }).show(); } }); srcList.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { JListUtil.newInstance(srcList).defaultJListKeyPressed(evt); } }); } } { addDirFiles = new JButton(); jPanel1.add(addDirFiles, BorderLayout.NORTH); addDirFiles.setText("add dir files"); addDirFiles.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { File file = JFileChooserUtil.newInstance().selectDirectoryOnly().showOpenDialog() .getApproveSelectedFile(); if (file == null || !file.isDirectory()) { return; } List<File> fileLst = new ArrayList<File>(); String subName = StringUtils.trimToEmpty(subFileNameText.getText()); if (StringUtils.isBlank(subName)) { subName = ".*"; } String patternStr = ".*\\." + subName; if (childrenDirChkbox.isSelected()) { FileUtil.searchFileMatchs(file, patternStr, fileLst); } else { for (File f : file.listFiles()) { if (f.isFile() && f.getName().matches(patternStr)) { fileLst.add(f); } } } DefaultListModel model = new DefaultListModel(); for (File f : fileLst) { model.addElement(f); } srcList.setModel(model); } }); } } { jPanel2 = new JPanel(); BorderLayout jPanel2Layout = new BorderLayout(); jPanel2.setLayout(jPanel2Layout); jTabbedPane1.addTab("param", null, jPanel2, null); { exeucte = new JButton(); jPanel2.add(exeucte, BorderLayout.SOUTH); exeucte.setText("exeucte"); exeucte.setPreferredSize(new java.awt.Dimension(491, 125)); exeucte.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { exeucteActionPerformed(evt); } }); } { jPanel3 = new JPanel(); GroupLayout jPanel3Layout = new GroupLayout((JComponent) jPanel3); jPanel3.setLayout(jPanel3Layout); jPanel2.add(jPanel3, BorderLayout.CENTER); { repFromText = new JTextField(); } { repToText = new JTextField(); } jPanel3Layout.setHorizontalGroup(jPanel3Layout.createSequentialGroup() .addContainerGap(25, 25) .addGroup(jPanel3Layout.createParallelGroup() .addGroup(jPanel3Layout.createSequentialGroup().addComponent(repFromText, GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel3Layout.createSequentialGroup().addComponent(repToText, GroupLayout.PREFERRED_SIZE, 446, GroupLayout.PREFERRED_SIZE))) .addContainerGap(20, Short.MAX_VALUE)); jPanel3Layout.setVerticalGroup(jPanel3Layout.createSequentialGroup().addContainerGap() .addComponent(repFromText, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(repToText, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE) .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)); } { addToTemplate = new JButton(); jPanel2.add(addToTemplate, BorderLayout.NORTH); addToTemplate.setText("add to template"); addToTemplate.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { prop.put(repFromText.getText(), repToText.getText()); reloadTemplateList(); } }); } } { jPanel4 = new JPanel(); BorderLayout jPanel4Layout = new BorderLayout(); jPanel4.setLayout(jPanel4Layout); jTabbedPane1.addTab("result", null, jPanel4, null); { jScrollPane2 = new JScrollPane(); jPanel4.add(jScrollPane2, BorderLayout.CENTER); { ListModel newRepListModel = new DefaultListModel(); newRepList = new JList(); jScrollPane2.setViewportView(newRepList); newRepList.setModel(newRepListModel); newRepList.addMouseListener(new MouseAdapter() { static final String tortoiseMergeExe = "TortoiseMerge.exe"; static final String commandFormat = "cmd /c call \"%s\" /base:\"%s\" /theirs:\"%s\""; public void mouseClicked(MouseEvent evt) { if (!JListUtil.newInstance(newRepList).isCorrectMouseClick(evt)) { return; } OldNewFile oldNewFile = (OldNewFile) JListUtil .getLeadSelectionObject(newRepList); String base = oldNewFile.newFile.getAbsolutePath(); String theirs = oldNewFile.oldFile.getAbsolutePath(); String command = String.format(commandFormat, tortoiseMergeExe, base, theirs); System.out.println(command); try { Runtime.getRuntime().exec(command); } catch (IOException e) { JCommonUtil.handleException(e); } } }); newRepList.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { Object[] objects = (Object[]) newRepList.getSelectedValues(); if (objects == null || objects.length == 0) { return; } DefaultListModel model = (DefaultListModel) newRepList.getModel(); int lastIndex = model.getSize() - 1; Object swap = null; StringBuilder dsb = new StringBuilder(); for (Object current : objects) { int index = model.indexOf(current); switch (evt.getKeyCode()) { case 38:// up if (index != 0) { swap = model.getElementAt(index - 1); model.setElementAt(swap, index); model.setElementAt(current, index - 1); } break; case 40:// down if (index != lastIndex) { swap = model.getElementAt(index + 1); model.setElementAt(swap, index); model.setElementAt(current, index + 1); } break; case 127:// del OldNewFile current_ = (OldNewFile) current; dsb.append(current_.newFile.getName() + "\t" + (current_.newFile.delete() ? "T" : "F") + "\n"); current_.newFile.delete(); model.removeElement(current); } } if (dsb.length() > 0) { JOptionPaneUtil.newInstance().iconInformationMessage() .showMessageDialog("del result!\n" + dsb, "DELETE"); } } }); } } { replaceOrignFile = new JButton(); jPanel4.add(replaceOrignFile, BorderLayout.SOUTH); replaceOrignFile.setText("replace orign file"); replaceOrignFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { DefaultListModel model = (DefaultListModel) newRepList.getModel(); StringBuilder sb = new StringBuilder(); for (int ii = 0; ii < model.size(); ii++) { OldNewFile file = (OldNewFile) model.getElementAt(ii); boolean delSuccess = false; boolean renameSuccess = false; if (delSuccess = file.oldFile.delete()) { renameSuccess = file.newFile.renameTo(file.oldFile); } sb.append(file.oldFile.getName() + " del:" + (delSuccess ? "T" : "F") + " rename:" + (renameSuccess ? "T" : "F") + "\n"); } JOptionPaneUtil.newInstance().iconInformationMessage().showMessageDialog(sb, getTitle()); } }); } } { jPanel5 = new JPanel(); BorderLayout jPanel5Layout = new BorderLayout(); jPanel5.setLayout(jPanel5Layout); jTabbedPane1.addTab("template", null, jPanel5, null); { jScrollPane3 = new JScrollPane(); jPanel5.add(jScrollPane3, BorderLayout.CENTER); { templateList = new JList(); reloadTemplateList(); jScrollPane3.setViewportView(templateList); templateList.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { if (templateList.getLeadSelectionIndex() == -1) { return; } Entry<Object, Object> entry = (Entry<Object, Object>) JListUtil .getLeadSelectionObject(templateList); repFromText.setText((String) entry.getKey()); repToText.setText((String) entry.getValue()); } }); templateList.addKeyListener(new KeyAdapter() { public void keyPressed(KeyEvent evt) { JListUtil.newInstance(templateList).defaultJListKeyPressed(evt); } }); } } { scheduleExecute = new JButton(); jPanel5.add(scheduleExecute, BorderLayout.SOUTH); scheduleExecute.setText("schedule execute"); scheduleExecute.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { scheduleExecuteActionPerformed(evt); } }); } } } this.setSize(512, 350); JCommonUtil.setFontAll(this.getRootPane()); JCommonUtil.frameCloseDo(this, new WindowAdapter() { public void windowClosing(WindowEvent paramWindowEvent) { if (StringUtils.isNotBlank(repFromText.getText())) { prop.put(repFromText.getText(), repToText.getText()); } try { prop.store(new FileOutputStream(propFile), "regexText"); } catch (Exception e) { JCommonUtil.handleException("properties store error!", e); } setVisible(false); dispose(); } }); } catch (Exception e) { e.printStackTrace(); } }