/* * Created by Nick on Oct 17, 2004 * */ package jmsltestsuite; import java.awt.*; import java.awt.event.*; import java.util.Date; import com.didkovsky.portview.*; import com.didkovsky.portview.awt.ViewFactoryAWT; import com.didkovsky.portview.swing.ViewFactorySwing; /** * Build a GUI with components that can switch between Swing and AWT . Change * flag in main() to choose Swing or AWT * * @author Nick Didkovsky, (c) 2004 All rights reserved, Email: * didkovn@mail.rockefeller.edu * */ public class PortViewTest implements ActionListener, ItemListener, MouseMotionListener { private PVFrame frame; private PVLabel messageLabel; private PVButton dateButton; private PVButton lineDrawingButton; private PVButton removeMenuButton; private PVButton toggleTextFieldButton; private PVCanvas canvas; private PVCheckbox checkbox1; private PVCheckbox checkbox2; private PVChoice choice; private PVMenu projectMenu; private PVMenuItem enableQuitMenuItem; private PVMenuItem quitMenuItem; private PVMenuBar menuBar; private PVMenu editMenu; private PVMenu submenuToBeRemoved; private PVMenuItem copyMenuItem; private PVCheckboxMenuItem checkboxMenuItem; private PVCheckbox radioButton1; private PVCheckbox radioButton2; private PVCheckbox radioButton3; private PVTextField textField; private LineDrawer lineDrawer; private ViewFactory viewFactory; public PortViewTest() { viewFactory = PortView.getViewFactory(); buildWindow(); buildMessageLabel(); buildButtons(); buildCanvas(); buildCheckboxes(); buildChoice(); buildMenus(); } public void show() { frame.pack(); frame.setVisible(true); } private void buildWindow() { String title = "View Factory building components in "; if (viewFactory.getFactoryType() == ViewFactory.SWING) title += "Swing"; else title += "AWT"; frame = viewFactory.createFrame(); frame.setTitle(title); frame.setFrameLayout(new BorderLayout()); } private void buildMessageLabel() { messageLabel = viewFactory.createLabel("I am a PVLabel. I can be Swing or AWT"); messageLabel.setAlignment(PVLabel.CENTER); frame.add(BorderLayout.SOUTH, messageLabel.getComponent()); } private void buildButtons() { PVPanel buttonPanel = viewFactory.createPanel(); buttonPanel.setLayout(new GridLayout(0, 1)); dateButton = viewFactory.createButton("Check the date"); lineDrawingButton = viewFactory.createButton("Start line drawing"); removeMenuButton = viewFactory.createButton("Remove a Menu"); toggleTextFieldButton = viewFactory.createButton("Toggle TextField"); buttonPanel.add(dateButton.getComponent()); buttonPanel.add(lineDrawingButton.getComponent()); buttonPanel.add(removeMenuButton.getComponent()); buttonPanel.add(toggleTextFieldButton.getComponent()); textField = viewFactory.createTextField("Type here and hit ENTER"); buttonPanel.add(textField.getComponent()); dateButton.addActionListener(this); lineDrawingButton.addActionListener(this); removeMenuButton.addActionListener(this); textField.addActionListener(this); toggleTextFieldButton.addActionListener(this); frame.add(BorderLayout.NORTH, buttonPanel.getComponent()); } private void buildCanvas() { canvas = viewFactory.createCanvas(); canvas.setCanvasSize(300, 300); frame.add(BorderLayout.CENTER, canvas.getComponent()); canvas.getComponent().addMouseMotionListener(this); } public void mouseDragged(MouseEvent ev) { } public void mouseMoved(MouseEvent ev) { if (lineDrawer != null) { lineDrawer.setXY(ev.getX(), ev.getY()); } } private void buildCheckboxes() { PVPanel checkboxPanel = viewFactory.createPanel(); checkboxPanel.setLayout(new GridLayout(0, 1)); // checkboxes checkbox1 = viewFactory.createCheckbox("Overdrive", true); checkbox2 = viewFactory.createCheckbox("Compressor", false); checkbox1.addItemListener(this); checkbox2.addItemListener(this); checkboxPanel.add(checkbox1.getComponent()); checkboxPanel.add(checkbox2.getComponent()); // radio buttons PVRadioGroup radioStationGroup = viewFactory.createRadioGroup(); radioButton1 = viewFactory.createCheckbox("89.9 FM (WKCR)", radioStationGroup, false); radioButton2 = viewFactory.createCheckbox("94.1 FM (KPFA)", radioStationGroup, false); radioButton3 = viewFactory.createCheckbox("91.1 FM (WFMU)", radioStationGroup, true); checkboxPanel.add(radioButton1.getComponent()); checkboxPanel.add(radioButton2.getComponent()); checkboxPanel.add(radioButton3.getComponent()); radioButton1.addItemListener(this); radioButton2.addItemListener(this); radioButton3.addItemListener(this); frame.add(BorderLayout.WEST, checkboxPanel.getComponent()); } private void buildChoice() { PVPanel choicePanel = viewFactory.createPanel(); choice = viewFactory.createChoice(); choice.addChoiceItem("Vanilla"); choice.addChoiceItem("Chocolate"); choice.addChoiceItem("Fish", true); choicePanel.add(choice.getComponent()); choice.addItemListener(this); frame.add(BorderLayout.EAST, choicePanel.getComponent()); } private void buildMenus() { menuBar = viewFactory.createMenuBar(); buildProjectMenu(); buildEditMenu(); frame.setPVMenuBar(menuBar); } private void buildEditMenu() { editMenu = viewFactory.createMenu("Edit"); submenuToBeRemoved = viewFactory.createMenu("I will be removed"); editMenu.add(submenuToBeRemoved); copyMenuItem = viewFactory.createMenuItem("Copy"); copyMenuItem.setKeyShortcut(KeyEvent.VK_C); copyMenuItem.addActionListener(this); editMenu.add(copyMenuItem); checkboxMenuItem = viewFactory.createCheckboxMenuItem("Audio", false); checkboxMenuItem.addItemListener(this); editMenu.add(checkboxMenuItem); menuBar.add(editMenu); } private void buildProjectMenu() { projectMenu = viewFactory.createMenu("Project"); enableQuitMenuItem = viewFactory.createMenuItem("Enable Quit"); enableQuitMenuItem.setKeyShortcut(KeyEvent.VK_E); quitMenuItem = viewFactory.createMenuItem("Quit"); quitMenuItem.setKeyShortcut(KeyEvent.VK_Q); quitMenuItem.setEnabled(false); enableQuitMenuItem.addActionListener(this); quitMenuItem.addActionListener(this); projectMenu.add(enableQuitMenuItem); projectMenu.add(quitMenuItem); menuBar.add(projectMenu); } public void actionPerformed(ActionEvent ev) { Object source = ev.getSource(); if (source == dateButton) { messageLabel.setText("" + new Date()); } else if (source == lineDrawingButton) { startLineDrawer(); } else if (source == choice) { PVChoice c = (PVChoice) ev.getSource(); messageLabel.setText("CHOICE: " + c.getSelectedChoiceItem() + ", " + c.getSelectedIndex()); } else if (source == enableQuitMenuItem) { quitMenuItem.setEnabled(true); messageLabel.setText("Quit Menu Item has been enabled"); } else if (source == quitMenuItem) { System.exit(0); } else if (source == copyMenuItem) { messageLabel.setText("Copying"); } else if (source == removeMenuButton) { editMenu.remove(submenuToBeRemoved); } else if (source == textField) { messageLabel.setText(textField.getText()); } else if (source == toggleTextFieldButton) { textField.setEnabled(!textField.isEnabled()); } } private void startLineDrawer() { lineDrawingButton.setEnabled(false); lineDrawer = new LineDrawer(); lineDrawer.setGraphics(canvas.getGraphics()); lineDrawer.start(); } public void itemStateChanged(ItemEvent ev) { System.out.println("itemStateChanged"); Object source = ev.getSource(); if (source == checkbox1 || source == checkbox2 || source == radioButton1 || source == radioButton2 || source == radioButton3) { handleCheckbox((PVCheckbox) source); } if (source == checkboxMenuItem) { if (checkboxMenuItem.getState()) messageLabel.setText("Audio on"); else messageLabel.setText("Audio off"); } if (source == choice) { messageLabel.setText((String) choice.getSelectedChoiceItem()); } } private void handleCheckbox(PVCheckbox checkbox) { messageLabel.setText(checkbox.getLabel() + ", " + checkbox.getState()); } public static void main(String[] args) { boolean useSwing = true; if (useSwing) { PortView.setViewFactory(new ViewFactorySwing()); } else { PortView.setViewFactory(new ViewFactoryAWT()); } PortViewTest vft = new PortViewTest(); vft.show(); } } class LineDrawer implements Runnable { private Graphics g; private Thread thread; private int x; private int y; public void setGraphics(Graphics g) { this.g = g; } public void setXY(int x, int y) { this.x = x; this.y = y; } public void start() { thread = new Thread(this); thread.start(); } public void run() { while (true) { g.setColor(new Color((float) Math.random(), (float) Math.random(), (float) Math.random())); g.drawLine(x, y, (int) (x + Math.random() * 40 - 20), (int) (y + Math.random() * 40 - 20)); try { Thread.sleep(100); } catch (InterruptedException e) { } } } }