void setPaths(String[] elements) throws ValidationException { if (!list.isEmpty()) { final int endIndex = list.size() - 1; list.clear(); fireIntervalRemoved(this, 0, endIndex); } final File[] files = new File[elements.length]; for (int i = 0; i < files.length; i++) { files[i] = new File(elements[i]); } addElements((Object[]) getSourceProducts()); addElements((Object[]) files); }
/** * Allows clients to add single products. * * @param product A product to add. */ public void addProduct(Product product) { if (product != null) { try { listModel.addElements(product); } catch (ValidationException ve) { Debug.trace(ve); } } }
@Override public void propertyChange(PropertyChangeEvent evt) { if (!internalPropertyChange) { Object newValue = evt.getNewValue(); try { if (newValue == null) { final Object[] sourceProducts = getSourceProducts(); clear(); addElements(sourceProducts); } else { setPaths((String[]) newValue); } } catch (ValidationException e) { SystemUtils.LOG.log(Level.SEVERE, "Problems at setPaths.", e); } } } });
@Test(expected = IllegalStateException.class) public void testFailingWithIllegalStateException() throws Exception { listModel.addElements(""); }
@Test public void testClear() throws Exception { listModel.addElements(new File("abc")); listModel.addElements(new File("def")); listModel.addElements(new Product("def", "producttype", 10, 10)); listModel.addElements(new Product("dummy", "producttype", 10, 10)); assertEquals(4, listModel.getSize()); listModel.clear(); assertEquals(0, listModel.getSize()); assertEquals(0, listModel.getSourceProducts().length); assertEquals(0, ((String[]) property.getValue()).length); }
@Override public void actionPerformed(ActionEvent e) { ProductChooser productChooser = new ProductChooser(appContext.getApplicationWindow(), "Add product", ModalDialog.ID_OK_CANCEL, null, filterProducts()); if (productChooser.show() != ModalDialog.ID_OK) { return; } try { if (productChooser.getSelectedProducts().size() > 0) { listModel.addElements(productChooser.getSelectedProducts().toArray()); } } catch (ValidationException ve) { Debug.trace(ve); } }
@Test public void testAddAndRemove() throws ValidationException { listModel.addElements(new File(".")); listModel.addElements(new File(".")); assertEquals(1, listModel.getSize()); listModel.removeElementsAt(new int[]{0}); assertEquals(0, listModel.getSize()); List<File> files = new ArrayList<>(); files.add(new File("abc")); files.add(new File("def")); files.add(new File("ghi")); listModel.addElements(files.toArray()); assertEquals(3, listModel.getSize()); assertEquals(3, ((String[]) property.getValue()).length); List<Product> products = new ArrayList<>(); products.add(new Product("abc", "meris", 10, 120)); products.add(new Product("def", "meris", 10, 120)); products.add(new Product("ghi", "meris", 10, 120)); listModel.addElements(products.toArray()); assertEquals(6, listModel.getSize()); assertEquals(3, listModel.getSourceProducts().length); listModel.removeElementsAt(new int[]{0, 5}); assertEquals(4, listModel.getSize()); assertEquals(2, ((String[]) property.getValue()).length); assertEquals(2, listModel.getSourceProducts().length); }
listModel.addElements(selectedProducts); } catch (ValidationException ve) {
listModel.addElements(selectedDirs); } catch (ValidationException ve) {
@Test public void testSetPropertyEmptyDoesNotDeleteSourceProducts() throws ValidationException { final Product product = new Product("name", "type", 1, 1); listModel.addElements(product); assertEquals(1, listModel.getSize()); final String[] elements1 = {"a", "b", "c"}; listModel.setPaths(elements1); assertEquals(4, listModel.getSize()); final String[] values1 = property.getValue(); assertArrayEquals(elements1, values1); property.setValue(null); assertEquals(1, listModel.getSize()); assertSame(product, listModel.getElementAt(0)); }