public void query(String query) throws IOException {
    EntrySet data;
    try {
      data = DatabaseService.getConnection().query(query);
    } catch (QueryException e) {
      e.printStackTrace();
      JOptionPane.showMessageDialog(
          Main.MAIN_FRAME, e.getMessage(), "Error Querying", JOptionPane.ERROR_MESSAGE);
      return;
    }

    removeAll();
    mainPanel.removeAll();
    toolbarPanel.removeAll();
    jtable = new JTable(new DataTableModel(data));
    jtable.setDefaultRenderer(Object.class, new DataTableRenderer());
    DataTableModel model = (DataTableModel) jtable.getModel();

    for (Entry entry : data.getEntries()) {
      model.addRow(entry.getData());
    }

    jtable.addMouseListener(new RowSelectionPopupListener(jtable, data));
    jtable
        .getModel()
        .addTableModelListener(
            new TableModelListener() {
              @Override
              public void tableChanged(TableModelEvent e) {
                if (!ignoreChanges) {
                  if (e.getType() == TableModelEvent.UPDATE) {
                    ignoreChanges = true;
                    int row = e.getLastRow();

                    if (row < data.size()) {
                      Entry entry = data.getEntries()[row];
                      Change change =
                          new Change(
                              entry.getEntryID(), Change.SET, new ArrayList<>(), new ArrayList<>());
                      Change previous = ChangeService.getService().getChange(entry.getEntryID());
                      if (previous != null) {
                        change.setOriginalData(previous.getOriginalData());
                      } else {
                        change.setOriginalData(new ArrayList<>(Arrays.asList(entry.getData())));
                      }

                      for (int col = 0; col < jtable.getColumnCount(); col++) {
                        change.getData().add((Serializable) jtable.getValueAt(row, col));
                      }

                      model.setRowColor(row, Color.orange);
                      ChangeService.getService().change(change);
                      setCanCommit(true);
                      ignoreChanges = false;
                    } else {
                      JOptionPane.showMessageDialog(
                          Main.MAIN_FRAME,
                          "You must commit this entry before editing it.",
                          "Error Editing",
                          JOptionPane.WARNING_MESSAGE);
                    }
                  }
                }
              }
            });

    JButton queryButton = new JButton("Query");
    queryField.setText(lastQuery);
    AutoCompleteDocument auto = new AutoCompleteDocument(queryField, data);

    queryField.getDocument().addDocumentListener(auto);
    queryField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_CONTROL, 0), "autocomplete");
    queryField.getActionMap().put("autocomplete", auto.getAutocompleteAction());
    mainPanel.add(queryField, 0, 0);
    mainPanel.c.anchor = GridBagConstraints.NORTHEAST;
    mainPanel.add(queryButton, 0, 0);

    queryButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            if (canCommit) {
              if (JOptionPane.showConfirmDialog(
                      Main.MAIN_FRAME,
                      "Requerying the table will remove your uncommited changes. Continue?",
                      "Discard Uncommited Changes?",
                      JOptionPane.YES_NO_CANCEL_OPTION)
                  != JOptionPane.YES_OPTION) {
                return;
              }
            }
            ChangeService.getService().setChanges(new ArrayList<>());
            setCanCommit(false);

            try {
              lastQuery = queryField.getText();
              DatabaseService.getConnection()
                  .query(queryField.getText() + " IN " + DatabaseService.getCurrentTableName());
              query(lastQuery.toLowerCase().startsWith("get") ? lastQuery : "GET");
            } catch (QueryException e1) {
              JOptionPane.showMessageDialog(
                  Main.MAIN_FRAME, e1.getMessage(), "Error Querying", JOptionPane.ERROR_MESSAGE);
            } catch (IOException e1) {
              e1.printStackTrace();
            }
            return;
          }
        });

    if (commitButton.getActionListeners().length > 0) {
      commitButton.removeActionListener(commitButton.getActionListeners()[0]);
    }
    commitButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            CommitChangesWindow window = new CommitChangesWindow(data);
            window.open();
          }
        });
    JButton addButton = new JButton(new ImageIcon("assets/GreenPlus.png"));
    addButton.addActionListener(
        new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            AddEntryWindow window = new AddEntryWindow(data);
            window.open();
          }
        });

    canCommit = false;
    commitButton.setEnabled(false);
    toolbarPanel.add(addButton, 0, 0);
    toolbarPanel.add(commitButton, 0, 1);

    addButton.setToolTipText("Add Entry");
    commitButton.setToolTipText("Commit Changes");

    mainPanel.c.anchor = GridBagConstraints.NORTHWEST;
    mainPanel.add(status, 0, 1);
    mainPanel.add(new JScrollPane(jtable), 0, 2);

    add(toolbarPanel);
    add(mainPanel);
    Main.MAIN_FRAME.pack();

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    Main.MAIN_FRAME.setLocation(
        dim.width / 2 - Main.MAIN_FRAME.getSize().width / 2,
        dim.height / 2 - Main.MAIN_FRAME.getSize().height / 2);
  }
 public void setStatus(String status) {
   this.status.setText(status);
   mainPanel.repaint();
 }
  public void setCanCommit(boolean canCommit) {
    this.canCommit = canCommit;

    commitButton.setEnabled(canCommit);
    mainPanel.repaint();
  }
 public void setQueryText(String query) {
   queryField.setText(query);
   lastQuery = query;
   mainPanel.repaint();
 }
Example #5
0
  public void init() {

    // IG.p("width="+getWidth()+", height="+getHeight());

    panel = new IGridPanel(0, 0, getWidth(), getHeight(), 2, 2);
    panel.setVisible(true);
    panel.setParent(this);
    // panel.setAdapter(this);

    ig = IG.init(panel);
    ig.server().graphicServer().enableGL();

    addMouseListener(panel);
    addMouseMotionListener(panel);
    addMouseWheelListener(panel);
    addKeyListener(panel);
    addFocusListener(panel);
    addComponentListener(panel);
    // addWindowListener(panel); // ?

    igg = new IGraphicsGL2();

    enableEvents(
        AWTEvent.MOUSE_EVENT_MASK
            | AWTEvent.MOUSE_MOTION_EVENT_MASK
            | AWTEvent.MOUSE_WHEEL_EVENT_MASK
        /*|AWTEvent.KEY_EVENT_MASK*/
        );

    initScreen(); // is it ok to put here?

    addComponentListener(
        new ComponentListener() {
          public void componentHidden(ComponentEvent e) {}

          public void componentMoved(ComponentEvent e) {}

          public void componentShown(ComponentEvent e) {}

          public void componentResized(ComponentEvent e) {
            setSize(getSize());
          }
        });

    setLayout(null);
    /*
    //GL
    GLProfile.initSingleton();
    final GLCanvas glcanvas = new GLCanvas();
    glcanvas.addGLEventListener(new GLEventListener(){
    	//@Override public void reshape( GLAutoDrawable glautodrawable, int x, int y, int w, int h){}
    	@Override public void init( GLAutoDrawable glautodrawable){}
    	@Override public void dispose( GLAutoDrawable glautodrawable){}
    	@Override public void display( GLAutoDrawable glautodrawable){
    	    glautodrawable.getGL().getGL2();
    	    glautodrawable.getWidth();
    	    glautodrawable.glHeight();
    	}
    	@Override public void displayChanged(GLAutoDrawable glautodrawable, boolean f1, boolean f2){}

        });
    glcanvas.setSize(getSize());
    add(glcanvas);
    //glanimationcontrol = new FPSAnimator(glacanvas, 30);
    */
    // final GLCanvas glcanvas = new GLCanvas();
    // IG.p(GL.GL_VERSION);
    // IG.p("Jogl package = "+Package.getPackage("javax.media.opengl"));
    // IG.p("JOGL version: "+ Package.getPackage("javax.media.opengl").getImplementationVersion());
  }