package de.fhro.gui.basic.uebung01; import java.awt.*; import java.awt.event.*; import java.util.ArrayList; import java.util.Iterator; import javax.sound.midi.ControllerEventListener; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField; public class JDBCBrowser { public static void main(String[] args) { BrowserFrame frame = new BrowserFrame(); frame.setVisible(true); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ System.exit(0); } }); } } class BrowserFrame extends Frame { private BrowserView view; public BrowserFrame() { view = new BrowserView(); add(view); super.pack(); } public BrowserView getBrowserView() { return view; } } class BrowserView extends Panel { private HeadingView heading; private CenterView center; private FooterView footer; // uebung 04 private SearchView sv; // ende uebung 04 private ApplicationController controller; public BrowserView() { setLayout(new BorderLayout()); heading = new HeadingView(); center = new CenterView(); controller = new ApplicationController(center); //footer = new FooterView(controller); sv = new SearchView("[0-9]+", "Label!!!"); sv.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); } } ); //end add(heading, BorderLayout.NORTH); add(center, BorderLayout.CENTER); //add(footer, BorderLayout.SOUTH); add(sv, BorderLayout.SOUTH); } public HeadingView getHeading() { return heading; } public void setHeading(HeadingView heading) { this.heading = heading; } public CenterView getCenter() { return center; } public void setCenter(CenterView center) { this.center = center; } public ApplicationController getController() { return controller; } public void setController(ApplicationController controller) { this.controller = controller; } public FooterView getFooter() { return footer; } public void setFooter(FooterView footer) { this.footer = footer; } } class HeadingView extends Panel { public HeadingView() { setLayout(new FlowLayout()); Label driverLbl = new Label("Driver:"); TextField driverInput = new TextField("oracle.djbc.driver.OracleDriver"); Label dbLbl = new Label("Database:"); TextField dbInput = new TextField("jdbc:oracle:thin:@141.60.120.130:1521"); Label userLbl = new Label("User:"); TextField userInput = new TextField("system"); // sollte pw verstecken Label passwordLbl = new Label("Password:"); TextField passwordInput = new TextField("manager"); add(driverLbl); add(driverInput); add(dbLbl); add(dbInput); add(userLbl); add(userInput); add(passwordLbl); add(passwordInput); } } class CenterView extends Panel { private CenterPane pane1; private CenterPane pane2; public CenterView() { setLayout(new GridLayout(0,1)); this.pane1 = new CenterPane("select * from article"); this.pane2 = new CenterPane(); add(pane1); add(pane2); } public CenterPane getUpperPane() { return pane1; } public CenterPane getLowerPane() { return pane2; } } class CenterPane extends ScrollPane { private TextArea textArea; public CenterPane() { super(ScrollPane.SCROLLBARS_ALWAYS); textArea = new TextArea("", 1, 1, TextArea.SCROLLBARS_NONE); add(textArea); } public CenterPane(String defaultText) { super(ScrollPane.SCROLLBARS_ALWAYS); textArea = new TextArea(defaultText, 1, 1, TextArea.SCROLLBARS_NONE); add(textArea); } public TextArea getTextArea(){ return this.textArea; } } class FooterView extends Panel { public FooterView(ApplicationController ac) { setLayout(new FlowLayout(FlowLayout.CENTER)); Button connectBtn = new Button("Connect"); connectBtn.setActionCommand("connect"); connectBtn.addActionListener(ac); Button infoBtn = new Button("DB-Info"); infoBtn.setActionCommand("info"); infoBtn.addActionListener(ac); Button submitBtn = new Button("Submit"); submitBtn.setActionCommand("submit"); submitBtn.addActionListener(ac); Button prepareBtn = new Button("Prepare"); prepareBtn.setActionCommand("prepare"); prepareBtn.addActionListener(ac); Button executeBtn = new Button("Execute"); executeBtn.setActionCommand("execute"); executeBtn.addActionListener(ac); Button commitBtn = new Button("Commit"); commitBtn.setActionCommand("commit"); commitBtn.addActionListener(ac); Button rollbackBtn = new Button("Rollback"); rollbackBtn.setActionCommand("rollback"); rollbackBtn.addActionListener(ac); add(connectBtn); add(infoBtn); add(submitBtn); add(prepareBtn); add(executeBtn); add(commitBtn); add(rollbackBtn); } } class SearchView extends JPanel { private JTextField termInput; private JButton searchBtn; private ArrayList listeners = new ArrayList(); private String regex; public void addActionListener(ActionListener l) { listeners.add(l); } public void removeActionListener(ActionListener l) { listeners.remove(l); } public SearchView(String regex, String label) { this.regex = regex; setLayout(new FlowLayout(FlowLayout.CENTER)); termInput = new JTextField(20); searchBtn = new JButton(label); searchBtn.setEnabled(false); Controller ctrl = new Controller(); termInput.addKeyListener(ctrl); searchBtn.addActionListener(ctrl); add(termInput); add(searchBtn); } private class Controller extends KeyAdapter implements ActionListener{ public void actionPerformed(ActionEvent e) { if (termInput.getText().matches(regex)) { fireActionEvent(new ActionEvent(SearchView.this, 0, termInput.getText())); } } public void keyReleased(KeyEvent e) { if (termInput.getText().matches(regex)) { searchBtn.setEnabled(true); } else { searchBtn.setEnabled(false); } } } private void fireActionEvent (ActionEvent e) { ArrayList l = (ArrayList) listeners.clone(); for (Iterator i = l.iterator(); i.hasNext();) { ((ActionListener) i.next()).actionPerformed(e); } } } class ApplicationController implements ActionListener { private Panel theView; public ApplicationController(Panel view) { this.theView = view; } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("connect")) { TextArea x = ((CenterView)this.theView).getUpperPane().getTextArea(); TextArea y = ((CenterView)this.theView).getLowerPane().getTextArea(); String toBeCopied = x.getText(); y.setText(toBeCopied); } if (e.getActionCommand().equals("info")) { System.out.println(e.getSource().toString()); } if (e.getActionCommand().equals("submit")) { System.out.println(e.getSource().toString()); } if (e.getActionCommand().equals("prepare")) { System.out.println(e.getSource().toString()); } if (e.getActionCommand().equals("execute")) { System.out.println(e.getSource().toString()); } if (e.getActionCommand().equals("commit")) { System.out.println(e.getSource().toString()); } if (e.getActionCommand().equals("rollback")) { System.out.println(e.getSource().toString()); } } }