Esempio n. 1
0
  /**
   * *********************************************** Initialize the Wing Body
   *
   * <p>The Wing Body always returns: -------------- Header Panel Content Panel ---------------
   */
  JPanel initWingBody() {

    /* Build each panel */
    wingPanelReference =
        new WingPanelReference(
            "This is the reference panel", wingFooter, cachedWidth, cachedHeight);

    String scriptPath = new String(Sketchbook.getSketchbookPath() + File.separator);
    wingPanelScripts = new WingPanelScripts(scriptPath, wingFooter, cachedWidth, cachedHeight);

    // wingPanelAppStore =  new WingPanelAppStore(	wingFooter,
    //											cachedWidth,
    //											cachedHeight);

    wingPanels[0] = wingPanelReference;
    wingPanels[1] = wingPanelScripts;
    // wingPanels[2] = wingPanelAppStore;
    // wingPanels[3] = initTestPanel("Test2");

    /* Retrieve the previous focused index */
    try {
      wingFocusedIndex = Preferences.getInteger("editor.rightWing.selectedTabIndex");
    } catch (Exception e) {
      wingFocusedIndex = 0;
    }

    // Error checking
    if (wingFocusedIndex >= wingTabs.length) {
      wingFocusedIndex = 0;
    }

    wingTabs[wingFocusedIndex].setFocused(true); // focus the tab
    return wingPanels[wingFocusedIndex];
  }
public class WingPanelScripts extends JPanel {

  boolean isUpdated = false;
  String text;
  Font font;

  private Color bgDefaultColor = Color.WHITE;
  private Color bgHoverColor = Color.WHITE;
  private Color bgColor;

  private Color textDefaultColor = Color.BLACK;
  private Color textHoverColor = Color.WHITE;
  private Color textColor;

  private double width = 300;
  private double height = 450;
  private boolean isFocused = false;
  private boolean isSelected = false;

  private String userdir = System.getProperty("user.dir") + File.separator;
  private String jrubyPath =
      new String(
          userdir
              + "hardware"
              + File.separator
              + "tools"
              + File.separator
              + "jruby"
              + File.separator
              + "bin"
              + File.separator);
  private String scriptPath = new String(Sketchbook.getSketchbookPath() + File.separator);
  private JPanel headerPanel;
  private JLabel headerLabel;
  private JList scriptFileList;
  private JScrollPane scrollPane;
  private WingFooter wingFooter;

  /**
   * Creates a new Wing Panel for scripts
   *
   * @param The directory where the scripts a located and executed from.
   * @param wingFooter The panel to display status to the user
   * @param width The width of the panel
   * @param height The height of the panel
   */
  public WingPanelScripts(String scriptDirectory, Object wingFooter, int width, int height) {

    this.width = width;
    this.height = height;
    this.wingFooter = (WingFooter) wingFooter;

    /* Setup the panel */
    setSize(width, height);
    setBackground(bgDefaultColor);
    setLayout(new BorderLayout());

    headerPanel = initPanelHeader(" | Run | ");

    setDirectory(scriptDirectory);

    /* Add */
    add(headerPanel, BorderLayout.NORTH);
    add(scrollPane, BorderLayout.CENTER);
  }

  /**
   * Initialize a Panel Header
   *
   * @param textDisplay The button text
   * @return: a JPanel with a button
   */
  JPanel initPanelHeader(String textDisplay) {

    JPanel headerPanel = new JPanel();
    headerPanel.setBackground(new Color(0x04, 0x4F, 0x6F));
    headerPanel.setLayout(new BorderLayout());

    headerLabel = new JLabel(textDisplay);

    headerLabel.addMouseListener(
        new MouseListener() {

          public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub

          }

          public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub
            headerLabel.setForeground(Color.orange);

            // TODO Auto-generated method stub
            String selectedItem = (String) scriptFileList.getSelectedValue();
            if (selectedItem != null) {
              wingFooter.setText(" Run " + selectedItem + " script");
            } else {
              wingFooter.setText(" Run script");
            }
          }

          public void mouseExited(MouseEvent arg0) {
            headerLabel.setForeground(Color.white);
          }

          public void mousePressed(MouseEvent arg0) {

            String selectedItem = (String) scriptFileList.getSelectedValue();
            if (selectedItem != null) {
              wingFooter.setText(" Running " + selectedItem + " script");
            } else {
              wingFooter.setText(" Run script");
            }
          }

          /* JRuby Script Execution
           */
          public void mouseReleased(MouseEvent arg0) {

            /* Run the selected script */
            executeJRubyScript((String) scriptFileList.getSelectedValue());
          }
        });

    headerLabel.setForeground(new Color(0xFF, 0xFF, 0xFF));
    headerPanel.add(headerLabel, BorderLayout.CENTER);

    return headerPanel;
  }

  /**
   * Initialize the panel's Body
   *
   * @param scriptList A list of files from the scriptDirectory
   */
  void initBody(String[] scriptList) {

    scriptFileList = new JList(scriptList);
    scriptFileList.setCellRenderer(new ScriptCellRenderer());

    scrollPane = new JScrollPane(scriptFileList);
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    scrollPane.setVisible(true);
  }

  /** Execute a JRuby Script If the script name is not null, this executes it. */
  void executeJRubyScript(String scriptName) {

    if (scriptName != null) {

      ScriptRunner myScript = new ScriptRunner(jrubyPath + "jruby.bat", jrubyPath + "jruby");

      try {
        myScript.run(scriptPath + scriptName);
        System.out.println(myScript.getScriptOutput());
        ((WingFooter) wingFooter).setText(" Script finished.");
      } catch (Exception e) {
        Base.showWarning("JRuby Error", "Could not find the JRuby Compiler\n", null);
        e.printStackTrace();
      }

    } else {
      /* Display the issue */
      wingFooter.setText("Select a script to run.");
    }
  }

  /** @param Sets the directory that scripts are read and executed from. */
  public void setDirectory(String scriptDirectory) {
    scriptPath = scriptDirectory;

    initBody(readScripts());

    wingFooter.setText("Scripts loaded from sketchbook directory.");
  }

  /** @return script files from the script directory */
  private String[] readScripts() {

    File scriptFolder = new File(scriptPath);
    String fileList[] =
        scriptFolder.list(
            new FilenameFilter() {

              public boolean accept(File dir, String name) {
                String extension = Utils.getExtension(name);
                if (extension != null) {
                  if (extension.equals(Utils.rb)) {
                    return true;
                  }
                }

                return false;
              }
            });

    return fileList;
  }
}