public void actionPerformed(ActionEvent e) {
int pos = textPane.getCaretPosition();
Action a = null;
if (e.getSource() == saveItem) {
FileDialog d = new FileDialog(new JFrame(), "Save file as ...", FileDialog.SAVE);
d.show();
if ((d.getDirectory() != null) && (d.getFile() != null)) {
try {
FileOutputStream out = new FileOutputStream(d.getDirectory()+d.getFile());
byte[] doc = textPane.getText().getBytes();
out.write(doc, 0, doc.length);
} catch (IOException ex) {
ex.printStackTrace();
}
}
} else if (e.getSource() == loadItem) {
FileDialog d = new FileDialog(new JFrame(), "Open file ...", FileDialog.LOAD);
d.show();
if ((d.getDirectory() != null) && (d.getFile() != null)) {
try {
textPane.setPage("file:"+d.getDirectory()+d.getFile());
}catch (IOException ex) {
ex.printStackTrace();
}
}
}
if (e.getSource() instanceof AbstractButton) {
if (e.getSource() == foregroundColor) {
JColorChooser cChooser = new JColorChooser();
JDialog d = JColorChooser.createDialog(this, "Choose foreground color ...", true, cChooser, this, this);
d.show();
if (cChooser.getColor() != null) {
Action ac = new StyledEditorKit.ForegroundAction(HTMLEditorKit.COLOR_ACTION,cChooser.getColor());
ac.actionPerformed(e);
}
} else if (e.getSource() == imgButton) {
try {
ImageChooser i = new ImageChooser(new JFrame());
i.show();
if (i.getSelectedFile() != null ) {
htmlEditorKit.insertHTML(htmlDoc, textPane.getCaretPosition(), "< img src="+i.getSelectedFile()+" >", 0,0,HTML.Tag.IMG);
}
}catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getSource() == boldButton) {
a = new StyledEditorKit.BoldAction();
} else if (e.getSource() == italicButton) {
a = new StyledEditorKit.ItalicAction();
} else if (e.getSource() == underlineButton) {
a = new StyledEditorKit.UnderlineAction();
} else if (e.getSource() == leftAl) {
a = new StyledEditorKit.AlignmentAction("Left", 0);
rightAl.setSelected(false);
centerAl.setSelected(false);
} else if (e.getSource() == rightAl) {
a = new StyledEditorKit.AlignmentAction("Right", 2);
leftAl.setSelected(false);
centerAl.setSelected(false);
} else if (e.getSource() == centerAl) {
a = new StyledEditorKit.AlignmentAction("Center", 1);
rightAl.setSelected(false);
leftAl.setSelected(false);
} else if (e.getSource() == tableButton) {
a = (Action)actions.get("InsertTable");
} else if (e.getSource() == tableRowButton) {
a = (Action)actions.get("InsertTableRow");
} else if (e.getSource() == tableCellButton) {
a = (Action)actions.get("InsertTableDataCell");
} else if (e.getSource() == unorderedListButton) {
a = (Action)actions.get("InsertUnorderedList");
}
} else if (e.getSource() instanceof JComboBox) {
if (e.getSource() == faceCombo) {
a = (Action)actions.get("font-family-"+(String)((JComboBox)e.getSource()).getSelectedItem());
} else if (e.getSource() == sizeCombo) {
a = (Action)actions.get("font-size-"+(String)((JComboBox)e.getSource()).getSelectedItem());
}
} // if (e.getSource() instanceof ... )
if (a != null) a.actionPerformed(e);
}
|