/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2012 Neil C Smith.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 3 for more details.
*
* You should have received a copy of the GNU General Public License version 3
* along with this work; if not, see http://www.gnu.org/licenses/
*
*
* Please visit http://neilcsmith.net if you need additional information or
* have any questions.
*/
package net.neilcsmith.praxis.live.pxr.editors;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import javax.swing.AbstractAction;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import net.neilcsmith.praxis.core.CallArguments;
import net.neilcsmith.praxis.core.types.PString;
import net.neilcsmith.praxis.live.core.api.Callback;
import net.neilcsmith.praxis.live.properties.PraxisProperty;
import net.neilcsmith.praxis.live.pxr.BoundArgumentProperty;
import net.neilcsmith.praxis.texteditor.TextEditor;
import org.openide.explorer.propertysheet.PropertyEnv;
/**
*
* @author Neil C Smith
*/
class CodeCustomEditor extends javax.swing.JPanel implements VetoableChangeListener {
private final CodeEditor editor;
private final PropertyEnv env;
private final String template;
private final TextEditor textEditor;
/**
* Creates new form CodeCustomEditor
*/
CodeCustomEditor(CodeEditor editor, PropertyEnv env, String mime, String template) {
initComponents();
this.editor = editor;
this.env = env;
this.template = template;
env.setState(PropertyEnv.STATE_NEEDS_VALIDATION);
env.addVetoableChangeListener(this);
String contents = editor.getAsText();
contents = contents.isEmpty() ? template : contents;
textEditor = TextEditor.create(mime, contents);
JComponent cmp = textEditor.getEditorComponent();
cmp.setPreferredSize(new Dimension(600, 400));
add(cmp, BorderLayout.CENTER);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_F9,0), "send");
getActionMap().put("send", new AbstractAction("send") {
@Override
public void actionPerformed(ActionEvent e) {
sendCurrent();
}
});
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
toolbar = new javax.swing.JToolBar();
revertButton = new javax.swing.JButton();
sendButton = new javax.swing.JButton();
messages = new javax.swing.JLabel();
setLayout(new java.awt.BorderLayout(3, 3));
toolbar.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
toolbar.setFloatable(false);
toolbar.setRollover(true);
revertButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/net/neilcsmith/praxis/live/pxr/resources/revert.png"))); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(revertButton, org.openide.util.NbBundle.getMessage(CodeCustomEditor.class, "CodeCustomEditor.revertButton.text")); // NOI18N
revertButton.setToolTipText(org.openide.util.NbBundle.getMessage(CodeCustomEditor.class, "CodeCustomEditor.revertButton.toolTipText")); // NOI18N
revertButton.setFocusable(false);
revertButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
revertButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
revertButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
revertButtonActionPerformed(evt);
}
});
toolbar.add(revertButton);
sendButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/net/neilcsmith/praxis/live/pxr/resources/play.png"))); // NOI18N
org.openide.awt.Mnemonics.setLocalizedText(sendButton, org.openide.util.NbBundle.getMessage(CodeCustomEditor.class, "CodeCustomEditor.sendButton.text")); // NOI18N
sendButton.setToolTipText(org.openide.util.NbBundle.getMessage(CodeCustomEditor.class, "CodeCustomEditor.sendButton.toolTipText")); // NOI18N
sendButton.setFocusable(false);
sendButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
sendButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
sendButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
sendButtonActionPerformed(evt);
}
});
toolbar.add(sendButton);
add(toolbar, java.awt.BorderLayout.PAGE_START);
messages.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
org.openide.awt.Mnemonics.setLocalizedText(messages, org.openide.util.NbBundle.getMessage(CodeCustomEditor.class, "CodeCustomEditor.messages.text")); // NOI18N
messages.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(messages, java.awt.BorderLayout.PAGE_END);
}// </editor-fold>//GEN-END:initComponents
private void revertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_revertButtonActionPerformed
revert();
}//GEN-LAST:event_revertButtonActionPerformed
private void revert() {
String contents = editor.getAsText();
contents = contents.isEmpty() ? template : contents;
textEditor.getTextComponent().setText(contents);
}
private void sendButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sendButtonActionPerformed
sendCurrent();
}//GEN-LAST:event_sendButtonActionPerformed
private void sendCurrent() {
String text = textEditor.getTextComponent().getText();
if (text.equals(template)) {
text = "";
}
PraxisProperty p = editor.getProperty();
if (p instanceof BoundArgumentProperty) {
BoundArgumentProperty prop = (BoundArgumentProperty) p;
prop.setValue(text.isEmpty() ? PString.EMPTY : PString.valueOf(text),
new Callback() {
@Override
public void onReturn(CallArguments args) {
messages.setText("OK");
}
@Override
public void onError(CallArguments args) {
messages.setText("ERROR");
}
});
messages.setText("Compiling ...");
} else {
messages.setText("");
editor.setAsText(text);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel messages;
private javax.swing.JButton revertButton;
private javax.swing.JButton sendButton;
private javax.swing.JToolBar toolbar;
// End of variables declaration//GEN-END:variables
@Override
public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
if (PropertyEnv.PROP_STATE.equals(evt.getPropertyName()) && evt.getNewValue() == PropertyEnv.STATE_VALID) {
String text = textEditor.getTextComponent().getText();
if (text.equals(template)) {
editor.setAsText("");
} else {
editor.setAsText(text);
}
}
}
}