package org.openslx.dozmod.gui.wizard.page;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;
import java.util.List;
import java.awt.Color;
import javax.swing.Action;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import org.apache.log4j.Logger;
import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.ShareMode;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.TextChangeListener;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageMetaDataPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.thrifthelper.Comparators;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.util.vm.QemuMetaData;
/**
* Page for setting the details of an image.
*/
@SuppressWarnings("serial")
public class ImageMetaDataPage extends ImageMetaDataPageLayout {
private final static Logger LOGGER = Logger.getLogger(ImageMetaDataPage.class);
private UploadWizardState state;
public ImageMetaDataPage(Wizard wizard, UploadWizardState uploadWizardState) {
super(wizard);
this.state = uploadWizardState;
setPageComplete(false);
// HACK set fixed uploadWizardState to test functions
uploadWizardState.shareMode = ShareMode.LOCAL;
chkIsTemplate.setEnabled(Session.isSuperUser());
// fetch the OS list
QuickTimer.scheduleOnce(new Task() {
List<OperatingSystem> osList = null;
@Override
public void fire() {
osList = MetaDataCache.getOperatingSystems();
// now send the organizations back to the LoginWindow
// through populateIdpCombo()
Gui.asyncExec(new Runnable() {
@Override
public void run() {
fillOsCombo(osList);
}
});
}
});
cboOperatingSystem.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
reactToUserInput();
}
}
});
txtDescription.getDocument().addDocumentListener(new TextChangeListener() {
@Override
public void changed() {
reactToUserInput();
}
});
cbTxtSize.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int size = Integer.parseInt((String) cbTxtSize.getSelectedItem());
Action act = new StyledEditorKit.FontSizeAction(String.valueOf(size), size);
act.actionPerformed(new ActionEvent(act, ActionEvent.ACTION_PERFORMED,
(String) act.getValue(Action.ACTION_COMMAND_KEY)));
}
});
cbTxtColor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String color = (String) cbTxtColor.getSelectedItem();
Action act = null;
switch (color) {
case "Black":
act = new StyledEditorKit.ForegroundAction("Black", Color.black);
break;
case "Blue":
act = new StyledEditorKit.ForegroundAction("Blue", Color.blue);
break;
case "Yellow":
act = new StyledEditorKit.ForegroundAction("Yellow", Color.yellow);
break;
case "Red":
act = new StyledEditorKit.ForegroundAction("Red", Color.red);
break;
case "Green":
act = new StyledEditorKit.ForegroundAction("Green", Color.green);
break;
}
act.actionPerformed(new ActionEvent(act, ActionEvent.ACTION_PERFORMED,
(String) act.getValue(Action.ACTION_COMMAND_KEY)));
}
});
btnWysiwyg.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String tmp = txtDescription.getText();
if (txtDescription.getContentType().equals("text/html")) {
txtDescription.setContentType("text/plain");
txtDescription.setText(tmp);
btnWysiwyg.setText("Wysiwyg");
btnBold.setEnabled(false);
btnUnderline.setEnabled(false);
btnItalic.setEnabled(false);
cbTxtColor.setEnabled(false);
cbTxtSize.setEnabled(false);
} else {
txtDescription.setContentType("text/html");
txtDescription.setText(tmp);
btnWysiwyg.setText("Html");
btnBold.setEnabled(true);
btnUnderline.setEnabled(true);
btnItalic.setEnabled(true);
cbTxtColor.setEnabled(true);
cbTxtSize.setEnabled(true);
}
}
});
txtDescription.addKeyListener(new KeyListener() {
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && txtDescription.getContentType().equals("text/html")) {
try {
kit.insertHTML((HTMLDocument) txtDescription.getDocument(), txtDescription.getCaretPosition(),
"<br>", 0, 0, HTML.Tag.BR);
txtDescription.setCaretPosition(txtDescription.getCaretPosition()); // This moves caret to next
// line
} catch (BadLocationException | IOException ex) {
ex.printStackTrace();
}
}
}
});
}
@Override
protected void onPageEnter() {
// Preselect OS if possible
if (state.detectedOs != null) {
cboOperatingSystem.setSelectedItem(state.detectedOs);
} else if (state.selectedOs == null) {
cboOperatingSystem.setSelectedItem(null);
}
sCommandCaption.setVisible(false);
startCommandPane.setVisible(false);
chkIsTemplate.setSelected(state.isTemplate);
chkLicenseRestricted.setSelected(state.isRestricted);
reactToUserInput();
}
@Override
protected boolean wantNextOrFinish() {
state.selectedOs = (OperatingSystem) cboOperatingSystem.getSelectedItem();
state.isTemplate = chkIsTemplate.isSelected();
state.isRestricted = chkLicenseRestricted.isSelected();
return state.selectedOs != null && state.description != null;
}
/**
* @param osList list of OS's to fill the combo with
*/
private void fillOsCombo(List<OperatingSystem> osList) {
Collections.sort(osList, Comparators.operatingSystemByName);
for (OperatingSystem os : osList) {
cboOperatingSystem.addItem(os);
}
}
/**
* Called by event listeners. This will set guidance message or error message
* and call setPageComplete(bool) accordingly.
*/
private void reactToUserInput() {
if (cboOperatingSystem.getSelectedIndex() == -1) {
// OS empty, description input present
setWarningMessage("Wählen Sie das Betriebssystem aus.");
setPageComplete(false);
return;
}
if (state.meta instanceof QemuMetaData) {
sCommandCaption.setVisible(true);
startCommandPane.setVisible(true);
}
// evaluate description field
state.description = txtDescription.getText();
if (state.description == null || state.description.isEmpty()) {
// OS set, no description
setWarningMessage("Fügen Sie eine Beschreibung hinzu.");
setPageComplete(false);
return;
}
setDescription("Klicken Sie auf 'Weiter' um Berechtigungen festzulegen oder 'Fertigstellen'");
setPageComplete(true);
}
}