Skip to content

Commit

Permalink
implemented actions in generate dialog. issue #2395
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Aug 19, 2024
1 parent bb127ec commit 467e38d
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</NonVisualComponents>
<Properties>
<Property name="defaultCloseOperation" type="int" value="2"/>
<Property name="title" type="java.lang.String" value="Text generieren (Diktat, Prompting)"/>
<Property name="title" type="java.lang.String" value="Texte generieren"/>
</Properties>
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
Expand Down Expand Up @@ -49,12 +49,11 @@
<Component id="cmdTranslate" min="-2" max="-2" attributes="0"/>
</Group>
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<Component id="cmdAddDocument" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cmdCancel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
<Component id="cmdCopy" min="-2" max="-2" attributes="0"/>
</Group>
<Component id="jSplitPane1" alignment="0" pref="788" max="32767" attributes="0"/>
<Component id="jSplitPane1" alignment="0" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
Expand All @@ -79,7 +78,7 @@
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="cmdCancel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cmdAddDocument" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="cmdCopy" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
Expand All @@ -92,23 +91,23 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons/cancel.png"/>
</Property>
<Property name="text" type="java.lang.String" value="Verwerfen"/>
<Property name="text" type="java.lang.String" value="Schliessen"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdCancelActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="cmdAddDocument">
<Component class="javax.swing.JButton" name="cmdCopy">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons/agt_action_success.png"/>
</Property>
<Property name="text" type="java.lang.String" value="Aufnahme speichern"/>
<Property name="text" type="java.lang.String" value="Kopieren"/>
<Property name="toolTipText" type="java.lang.String" value=""/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdAddDocumentActionPerformed"/>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdCopyActionPerformed"/>
</Events>
</Component>
<Container class="javax.swing.JPanel" name="jPanel2">
Expand Down Expand Up @@ -150,7 +149,7 @@
<Color blue="ff" green="ff" red="ff" type="rgb"/>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="Assistent Ingo: Diktieren und &#xdc;bersetzen"/>
<Property name="text" type="java.lang.String" value="Diktieren / Text generieren / &#xdc;bersetzen"/>
</Properties>
</Component>
</SubComponents>
Expand Down Expand Up @@ -190,6 +189,9 @@
<Properties>
<Property name="text" type="java.lang.String" value="&#xdc;bersetzen"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="cmdTranslateActionPerformed"/>
</Events>
</Component>
<Container class="javax.swing.JPanel" name="pnlParameters">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@
import com.jdimension.jlawyer.persistence.AssistantPrompt;
import com.jdimension.jlawyer.services.JLawyerServiceLocator;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -702,9 +705,9 @@
*
* @author jens
*/
public class AssistantDictateDialog extends javax.swing.JDialog implements AssistantFlowAdapter {
public class AssistantGenerateDialog extends javax.swing.JDialog implements AssistantFlowAdapter {

private static final Logger log = Logger.getLogger(AssistantDictateDialog.class.getName());
private static final Logger log = Logger.getLogger(AssistantGenerateDialog.class.getName());

private TargetDataLine targetDataLine;
private ByteArrayOutputStream byteArrayOutputStream;
Expand All @@ -718,7 +721,7 @@ public class AssistantDictateDialog extends javax.swing.JDialog implements Assis

private AiCapability transcribeCapability = null;
private AssistantConfig transcribeConfig = null;

private AiCapability generateCapability = null;
private AssistantConfig generateConfig = null;

Expand All @@ -728,7 +731,7 @@ public class AssistantDictateDialog extends javax.swing.JDialog implements Assis
* @param parent
* @param modal
*/
public AssistantDictateDialog(java.awt.Frame parent, boolean modal) {
public AssistantGenerateDialog(java.awt.Frame parent, boolean modal) {
super(parent, modal);
initComponents();

Expand Down Expand Up @@ -770,7 +773,7 @@ public AssistantDictateDialog(java.awt.Frame parent, boolean modal) {
log.error(ex);
JOptionPane.showMessageDialog(this, "" + ex.getMessage(), com.jdimension.jlawyer.client.utils.DesktopUtils.POPUP_TITLE_ERROR, JOptionPane.ERROR_MESSAGE);
}

try {
Map<AssistantConfig, List<AiCapability>> capabilities = ingo.filterCapabilities(AiCapability.REQUESTTYPE_GENERATE, AiCapability.INPUTTYPE_NONE);
if (capabilities.isEmpty()) {
Expand Down Expand Up @@ -800,7 +803,7 @@ public AssistantDictateDialog(java.awt.Frame parent, boolean modal) {
this.pnlParameters.add(tf);
}
}

ComponentUtils.decorateSplitPane(this.jSplitPane1);
ComponentUtils.restoreSplitPane(this.jSplitPane1, this.getClass(), "jSplitPane1");
ComponentUtils.persistSplitPane(this.jSplitPane1, this.getClass(), "jSplitPane1");
Expand Down Expand Up @@ -846,7 +849,7 @@ private void initComponents() {

popAssistant = new javax.swing.JPopupMenu();
cmdCancel = new javax.swing.JButton();
cmdAddDocument = new javax.swing.JButton();
cmdCopy = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
cmdRecord = new javax.swing.JButton();
Expand All @@ -864,28 +867,28 @@ private void initComponents() {
taResult = new javax.swing.JTextArea();

setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
setTitle("Text generieren (Diktat, Prompting)");
setTitle("Texte generieren");
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentResized(java.awt.event.ComponentEvent evt) {
formComponentResized(evt);
}
});

cmdCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
cmdCancel.setText("Verwerfen");
cmdCancel.setText("Schliessen");
cmdCancel.setToolTipText("");
cmdCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdCancelActionPerformed(evt);
}
});

cmdAddDocument.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/agt_action_success.png"))); // NOI18N
cmdAddDocument.setText("Aufnahme speichern");
cmdAddDocument.setToolTipText("");
cmdAddDocument.addActionListener(new java.awt.event.ActionListener() {
cmdCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/agt_action_success.png"))); // NOI18N
cmdCopy.setText("Kopieren");
cmdCopy.setToolTipText("");
cmdCopy.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdAddDocumentActionPerformed(evt);
cmdCopyActionPerformed(evt);
}
});

Expand All @@ -894,7 +897,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
jLabel1.setFont(jLabel1.getFont().deriveFont(jLabel1.getFont().getSize()+2f));
jLabel1.setForeground(new java.awt.Color(255, 255, 255));
jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
jLabel1.setText("Assistent Ingo: Diktieren und Übersetzen");
jLabel1.setText("Diktieren / Text generieren / Übersetzen");

org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
Expand Down Expand Up @@ -925,6 +928,11 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
cmbDevices.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

cmdTranslate.setText("Übersetzen");
cmdTranslate.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cmdTranslateActionPerformed(evt);
}
});

org.jdesktop.layout.GroupLayout pnlParametersLayout = new org.jdesktop.layout.GroupLayout(pnlParameters);
pnlParameters.setLayout(pnlParametersLayout);
Expand Down Expand Up @@ -1015,11 +1023,10 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(cmdTranslate))
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(0, 0, Short.MAX_VALUE)
.add(cmdAddDocument)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(cmdCancel))
.add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 788, Short.MAX_VALUE))
.add(cmdCancel)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(cmdCopy))
.add(jSplitPane1))
.addContainerGap())
);
layout.setVerticalGroup(
Expand All @@ -1040,7 +1047,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(cmdCancel)
.add(cmdAddDocument))
.add(cmdCopy))
.addContainerGap())
);

Expand All @@ -1053,22 +1060,15 @@ private void cmdCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIR
this.dispose();
}//GEN-LAST:event_cmdCancelActionPerformed

private void cmdAddDocumentActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddDocumentActionPerformed

// try {
//
// if (!this.memoParts.isEmpty()) {
//
// this.memoBytes = mergeWAVs(this.memoParts);
//
// }
// } catch (Exception e) {
// log.error(e);
//
// }
private void cmdCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCopyActionPerformed

Toolkit toolkit = Toolkit.getDefaultToolkit();
Clipboard clipboard = toolkit.getSystemClipboard();
StringSelection strSel = new StringSelection(this.taResult.getText());
clipboard.setContents(strSel, null);
this.setVisible(false);
this.dispose();
}//GEN-LAST:event_cmdAddDocumentActionPerformed
}//GEN-LAST:event_cmdCopyActionPerformed

private byte[] generateWAV(byte[] audio) throws IOException, UnsupportedAudioFileException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -1142,16 +1142,16 @@ private void cmdPromptMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:
try {
AssistantAccess ingo = AssistantAccess.getInstance();
this.popAssistant.removeAll();
List<AssistantPrompt> customPrompts=ingo.getCustomPrompts(AiCapability.REQUESTTYPE_GENERATE);
for(AssistantPrompt p: customPrompts) {
List<AssistantPrompt> customPrompts = ingo.getCustomPrompts(AiCapability.REQUESTTYPE_GENERATE);
for (AssistantPrompt p : customPrompts) {
JMenuItem mi = new JMenuItem();
mi.setText(p.getName());
mi.addActionListener((ActionEvent e) -> {
this.taPrompt.setText(p.getPrompt());
});
popAssistant.add(mi);
}

this.popAssistant.show(this.cmdPrompt, evt.getX(), evt.getY());
} catch (Exception ex) {
log.error(ex);
Expand Down Expand Up @@ -1224,18 +1224,88 @@ protected void done() {
}
}//GEN-LAST:event_cmdExecutePromptActionPerformed

private void cmdTranslateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTranslateActionPerformed
AssistantAccess ingo = AssistantAccess.getInstance();
try {

List<ParameterData> params = new ArrayList<>();
if (translateCapability.getParameters() != null && !translateCapability.getParameters().isEmpty()) {
params = getParameters(translateCapability);
}

final List<ParameterData> fParams = params;

this.progress.setIndeterminate(true);

AtomicReference<AiRequestStatus> resultRef = new AtomicReference<>();

SwingWorker<Void, Void> worker = new SwingWorker<>() {

@Override
protected Void doInBackground() throws Exception {
List<InputData> inputs = new ArrayList<>();
InputData i = new InputData();
i.setType(InputData.TYPE_STRING);
i.setBase64(false);
i.setStringData(taResult.getText());
inputs.add(i);

ClientSettings settings = ClientSettings.getInstance();
try {
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());

AiRequestStatus status = locator.lookupIntegrationServiceRemote().submitAssistantRequest(translateConfig, translateCapability.getRequestType(), translateCapability.getModelType(), taPrompt.getText(), fParams, inputs);
resultRef.set(status);

} catch (Throwable t) {
log.error("Error processing AI request", t);
}
return null;
}

@Override
protected void done() {
// Task completion actions
AiRequestStatus status = resultRef.get();
if (status != null) {
if (status.getStatus().equalsIgnoreCase("error")) {
taResult.insert(status.getStatus() + ": " + status.getStatusDetails(), taResult.getCaretPosition());
} else {
StringBuilder resultString = new StringBuilder();
for (OutputData o : status.getResponse().getOutputData()) {
if (o.getType().equalsIgnoreCase(OutputData.TYPE_STRING)) {
resultString.append(o.getStringData()).append(System.lineSeparator()).append(System.lineSeparator());
}

}
taResult.insert(resultString.toString(), taResult.getCaretPosition());
}
}

progress.setIndeterminate(false);
}
};

worker.execute();

} catch (Exception ex) {
log.error(ex);
JOptionPane.showMessageDialog(this, "" + ex.getMessage(), com.jdimension.jlawyer.client.utils.DesktopUtils.POPUP_TITLE_ERROR, JOptionPane.ERROR_MESSAGE);
}
}//GEN-LAST:event_cmdTranslateActionPerformed

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(() -> {
new AssistantDictateDialog(new javax.swing.JFrame(), true).setVisible(true);
new AssistantGenerateDialog(new javax.swing.JFrame(), true).setVisible(true);
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JComboBox<String> cmbDevices;
private javax.swing.JButton cmdAddDocument;
private javax.swing.JButton cmdCancel;
private javax.swing.JButton cmdCopy;
private javax.swing.JButton cmdExecutePrompt;
private javax.swing.JButton cmdPrompt;
private javax.swing.JButton cmdRecord;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<Component class="javax.swing.JButton" name="cmdClose">
<Properties>
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/icons/agt_action_success.png"/>
<Image iconType="3" name="/icons/cancel.png"/>
</Property>
<Property name="text" type="java.lang.String" value="Schliessen"/>
</Properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});

cmdClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/agt_action_success.png"))); // NOI18N
cmdClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancel.png"))); // NOI18N
cmdClose.setText("Schliessen");
cmdClose.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down
Loading

0 comments on commit 467e38d

Please sign in to comment.