package com.ericsson.tic.vi; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.BoxLayout; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.ScrollPaneConstants; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JColorChooser; import javax.swing.ImageIcon; import java.awt.image.BufferedImage; import java.awt.Graphics2D; import java.io.File; import javax.imageio.ImageIO; import java.io.IOException; import java.util.LinkedList; import java.util.ListIterator; /** * The DataListPanel and DataPanel combine to create lists of * categories/views etc. * * @see CardPanel * @see DataPanel * @author Sami Matilainen * @version 1.0 (2008-12-04) */ public class DataListPanel extends JPanel implements ActionListener { /** Subpanel used for layout. */ private JPanel topPanel; /** A Jpanel used for layout. */ private JPanel container; /** A scrollpane. */ private JScrollPane scrollPane; /** A list of datapanels. */ private DataPanel[] dataPanel; /** The type of this data list. */ public int type; /** * Creates a Data List. * * @param type The type of this data list. Defined in CardPanel. */ public DataListPanel(int type) { this.type = type; this.setLayout(new BorderLayout()); this.setPreferredSize(new Dimension(350, 350)); this.setMinimumSize(new Dimension(350, 350)); this.setMaximumSize(new Dimension(350, 350)); container = new JPanel(); container.setLayout(new BorderLayout()); topPanel = new JPanel(); topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.PAGE_AXIS)); if (type == CardPanel.CATEGORY) { listCategories(); } else if (type == CardPanel.NODE) { listNodes(); } else if (type == CardPanel.REL) { listRelations(); } else if (type == CardPanel.VIEW) { listViews(); } container.add(topPanel, BorderLayout.NORTH); container.add(new JPanel(), BorderLayout.CENTER); scrollPane = new JScrollPane(container); scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.add(scrollPane, BorderLayout.CENTER); } /** * Creates a category list. */ public void listCategories() { // note that this mrthod currently only lists the first category! // todo: loop thru the categories and list each one in it's own "tab" // similar to how view-settings are listed. dataPanel = new DataPanel[VI.categoryArr[1].cats.length]; for (int i = 0; i < VI.categoryArr[1].cats.length; i++) { Color c = new Color( VI.categoryArr[1].cats[i].color[0], VI.categoryArr[1].cats[i].color[1], VI.categoryArr[1].cats[i].color[2]); dataPanel[i] = new DataPanel( VI.categoryArr[1].cats[i].name, c, this); topPanel.add(dataPanel[i]); dataPanel[i].colorButton.removeActionListener( dataPanel[i].colorButton); dataPanel[i].colorButton.addActionListener(this); dataPanel[i].colorButton.setActionCommand("category " + i); dataPanel[i].brushingButton.addActionListener(this); dataPanel[i].brushingButton.setActionCommand("brush category " + i); dataPanel[i].colorButton.brushingEnabled = true; } } /** * Creates a view list. */ public void listViews() { dataPanel = new DataPanel[VI.viewList.size()]; ListIterator iter = VI.viewList.listIterator(); ViewPanel view; int i = 0; while (iter.hasNext()) { view = (ViewPanel)iter.next(); dataPanel[i] = new DataPanel("View " + view.id, this, view); topPanel.add(dataPanel[i]); i++; } } /** * Updates the view list. */ public void updateViewList() { this.removeAll(); container = new JPanel(); container.setLayout(new BorderLayout()); topPanel = new JPanel(); topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.PAGE_AXIS)); dataPanel = new DataPanel[VI.viewList.size()]; ListIterator iter = VI.viewList.listIterator(); ViewPanel view; int i = 0; while (iter.hasNext()) { view = (ViewPanel)iter.next(); dataPanel[i] = new DataPanel("View " + view.id, this, view); topPanel.add(dataPanel[i]); i++; } container.add(topPanel, BorderLayout.NORTH); container.add(new JPanel(), BorderLayout.CENTER); scrollPane = new JScrollPane(container); scrollPane.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); this.add(scrollPane, BorderLayout.CENTER); //this.validate(); this.revalidate(); } /** * Creates a relations list. This method is empty. */ public void listRelations() { // todo: list all relations // todo: settings for the relations } /** * Creates a nodes list. This method is empty. */ public void listNodes() { // todo: list all nodes // todo: settings for the nodes } /** * Called when an ActionEvent is triggered. * * @param e The ActionEvent which triggered the event. */ public void actionPerformed(ActionEvent e) { for (int i = 0; i < VI.categoryArr[1].cats.length; i++) { if (("category " + i).equals(e.getActionCommand())) { Color color = JColorChooser.showDialog( this, "Choose color", dataPanel[i].colorButton.c); if (color != null) { dataPanel[i].colorButton.c = color; dataPanel[i].colorButton.updateIcon(); } VI.categoryArr[1].cats[i].color = color.getColorComponents( VI.categoryArr[1].cats[i].color); } } for (int i = 0; i < VI.categoryArr[1].cats.length; i++) { if (("brush category " + i).equals(e.getActionCommand())) { for (int k = 0; k < VI.categoryArr[1].cats.length; k++) { dataPanel[k].colorButton.brushingPrimary = false; dataPanel[k].brushingButton.setIcon(VI.brushOffIcon); } if (VI.brushingOn && i == VI.brushedCategory) { VI.brushingOn = false; dataPanel[i].brushingButton.setIcon(VI.brushOffIcon); } else { VI.brushedCategory = i; VI.brushingOn = true; dataPanel[i].colorButton.brushingPrimary = true; dataPanel[i].brushingButton.setIcon(VI.brushOnIcon); } for (int k = 0; k < VI.categoryArr[1].cats.length; k++) { dataPanel[k].colorButton.updateIcon(); } } } } }