Package fbench.graph

Source Code of fbench.graph.AlgorithmComponent

//Copyright (c) 2007 University of Auckland
//Contributed to the FBench extension of the OOONEIDA Workbench project under the Common Public License

package fbench.graph;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import fbench.LibraryElementView;
import fbench.tree.DOMTree;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
* GraphElement which provides the Language selection, Save, Cancel,
* and Display/Eedit of the algorithm in Text form
* @author JP
* @version 20070131/JP
*/
public class AlgorithmComponent extends GraphElement{
  /**
   *
   */
  private static final long serialVersionUID = -4909384609257181481L;
  private CodeTextArea algTextArea = new CodeTextArea();
  private JScrollPane scrPane = new JScrollPane();
  private JPanel topPane = new JPanel(new BorderLayout(10, 0));
  private JPanel centerPane = new JPanel(new BorderLayout());
  private JPanel bottomPane = new JPanel();
  private JButton saveButton = new JButton("Save");
  private JButton cancelButton = new JButton("Cancel");
  private JLabel title = new JLabel();
  private static final String[] algElements = {"FBD", "Other", "Other", "LD", "ST", "Other"};
  private String[] algTypeList = {"Function Block Diagram", "Java", "C", "Ladder Logic Diagram", "Structured Text", "Other"};
  private JComboBox algTypeComboBox = new JComboBox(algTypeList);
  private JPanel algorithmTypeGrp = new JPanel();
  private JLabel prototypeLbl = new JLabel("Prototype:");
  private JTextField prototypeText = new JTextField();
  private String algType = new String();

  private DOMTree tree;

  private Element algorithmEl;
  public AlgorithmComponent(Element el){
    super();
    prototypeLbl.setPreferredSize(new Dimension(80,20));
    prototypeText.setPreferredSize(new Dimension(200,20));
    algorithmTypeGrp.add(algTypeComboBox);
   
    setElement(el); // calls initComponents()

    algorithmEl = (Element)el.getChildNodes().item(0);
    String algText = algorithmEl.getAttribute("Text");
    String algName = getElement().getAttribute("Name");

    algType = getElType();
    if(algType.equals("other")){
      if(algorithmEl.getAttribute("Language").toLowerCase().equals("java"))
        algType = "java";
      else if(algorithmEl.getAttribute("Language").toLowerCase().equals("c"))
        algType = "c";
      else
        algType = "other";
    }
    if(algType.equals("st") || algType.equals("java") || algType.equals("c")){
      setAlgorithmText(algText);
    }
    else if(algType.equals("fbd")){}
    else if(algType.equals("ld")){}
    else if(algType.equals("other")){}
   
    setAlgorithmName(algName);
    setComboBoxSelectedIndex(algType);

    BorderLayout borderLayout = new BorderLayout();
    borderLayout.setVgap(5);
    setLayout(borderLayout);
    this.setToolTipText(null);

    //algTextArea.setTabSize(2);
    algTextArea.addKeyListener(new KeyListener(){
      public void keyPressed(KeyEvent evt) {}

      public void keyReleased(KeyEvent evt) {}

      public void keyTyped(KeyEvent evt) {
        // TODO: gsha041: Causes issues & doesnt DISABLE use of tree
        // Fixed DOMTree -- remove mouse listener (it is a 'light-weight' component so it still listens to the mouse when disabled)
        LibraryElementView libraryElementView =
          (LibraryElementView)getParent().getParent().getParent().getParent().getParent().getParent();
        tree = libraryElementView.getTree();
        tree.setEnabled(false);
        tree.setRendererEnabled(false);
      }
    });

    saveButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt) {
        if(algType.equals("st") || algType.equals("java") || algType.equals("c")){
          algorithmEl.setAttribute("Text", algTextArea.getText());
        }
        if(algType.equals("c")){
          algorithmEl.setAttribute("Prototype", prototypeText.getText());
        }
        if(tree != null){
          tree.setEnabled(true);
          tree.setRendererEnabled(true);
        }
      }
    });

    cancelButton.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt) {
        algTextArea.setText(algorithmEl.getAttribute("Text"));
        if(tree != null){
          tree.setEnabled(true);
          tree.setRendererEnabled(true);
        }
      }
    });
    algTypeComboBox.addItemListener(new ItemListener(){
      public void itemStateChanged(ItemEvent itemEvt) {
        if(algTypeComboBox.getSelectedItem().equals(itemEvt.getItem())){
          //System.out.println("[AC] algType changed");
          String selectedItem = itemEvt.getItem().toString();
          // If a change from previous
          if( !selectedItem.equals(getElType()) )
          {
           
           
            getElement().removeChild((Node)algorithmEl);
           
            if( selectedItem.equals("Java") || selectedItem.equals("C") ){
              algType = "other";
              scrPane.setViewportView(algTextArea);
            } else if(selectedItem.equals("Structured Text")){
              algType = "st";
              scrPane.setViewportView(algTextArea);
            } else if(selectedItem.equals("Function Block Diagram")){
              algType = "fbd";
              scrPane.setViewportView(null);
            } else if(selectedItem.equals("Ladder Logic Diagram")){
              algType = "ld";
              scrPane.setViewportView(null);
            } else if(selectedItem.equals("Other")){
              algType = "other";
              scrPane.setViewportView(algTextArea);
            }
           
            // Redo element...
            algorithmEl = getElement().getOwnerDocument().createElement(algElements[algTypeComboBox.getSelectedIndex()]);
            if( selectedItem.equals("Java") || selectedItem.equals("C") )
              {
                algorithmEl.setAttribute("Language", selectedItem);
              }
            getElement().appendChild(algorithmEl);
            setElText(algTextArea.getText());
           
            if( algorithmEl.getAttribute("Language").equals("C") )
            {
              // Add prototype input
              prototypeText.setText( algorithmEl.getAttribute("Prototype") );
              algorithmTypeGrp.add(prototypeLbl);
              algorithmTypeGrp.add(prototypeText);
             
              algorithmTypeGrp.repaint();
            }
            else
            {
              algorithmTypeGrp.remove(prototypeLbl);
              algorithmTypeGrp.remove(prototypeText);
              algorithmTypeGrp.repaint();
            }
          }
        }
      }
    });
   
    if( algorithmEl != null && algorithmEl.getAttribute("Language").equals("C") )
    {
      // Add prototype input
      prototypeText.setText( algorithmEl.getAttribute("Prototype") );
     
      algorithmTypeGrp.add(prototypeLbl);
      algorithmTypeGrp.add(prototypeText);
     
      algorithmTypeGrp.repaint();
    }
   
  }

  protected void initComponents(){
    title.setFont(new Font("Verdana", Font.BOLD, 14));
    title.setForeground(new Color(2, 49, 87));
    title.setBackground(Color.white);
    title.setToolTipText(title.getText().replace("Algorithm: ", ""));
    title.setOpaque(true);

    topPane.setOpaque(false);
    topPane.setBackground(Color.white);
    bottomPane.setOpaque(false);
    bottomPane.setBackground(Color.white);
    FlowLayout bottomPaneLayout = new FlowLayout();
    bottomPaneLayout.setAlignment(FlowLayout.RIGHT);
    bottomPane.setLayout(bottomPaneLayout);

    topPane.add(title, BorderLayout.WEST);
    topPane.add(algorithmTypeGrp, BorderLayout.EAST);
   
    if(algType.equals("st") || algType.equals("java") || algType.equals("c") ){
      scrPane.setViewportView(algTextArea);
    } else{
      scrPane.setViewportView(null);
    }
    centerPane.add(scrPane, BorderLayout.CENTER);
    bottomPane.add(saveButton);
    bottomPane.add(cancelButton);

    add(topPane, BorderLayout.NORTH);
    add(centerPane, BorderLayout.CENTER);
    add(bottomPane, BorderLayout.SOUTH);
  }

  private void setComboBoxSelectedIndex(String type){
    int index = -1;
    type = type.toLowerCase();
    if(type.equals("fbd"))
      index = 0;
    else if(type.equals("java"))
      index = 1;
    else if(type.equals("c"))
      index = 2;
    else if(type.equals("ld"))
      index = 3;
    else if(type.equals("st"))
      index = 4;
    else if(type.equals("other"))
      index = 5;
    else index = -1;
    algTypeComboBox.setSelectedIndex(index);
  }

  public void setAlgorithmText(String text){
    algTextArea.setText(text);
  }
  public void setAlgorithmName(String name){
    title.setText("Algorithm: "+name);
  }

  public void setElText(String text){
    algorithmEl.setAttribute("Text", text);
  }
  public void setElName(String name){
    getElement().setAttribute("Name", name);
  }
  public String getElType(){
    return algorithmEl.getTagName().toLowerCase();
  }
 
  public Rectangle getPreferredBounds(){
    int dividerLocation = ((JSplitPane)getParent().getParent().getParent().getParent()).getDividerLocation();
    Dimension parentSize = getParent().getSize();
    Rectangle prefBounds = new Rectangle(10, 10,
        dividerLocation -24, parentSize.height - 15);
    return prefBounds;
  }

  public Integer getPreferredLayer() {
    return GraphView.DEFAULT_LAYER;
  }
}
TOP

Related Classes of fbench.graph.AlgorithmComponent

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.