[Kafenio-cvs-commit] kafenio_main/src/de/xeinfach/kafenio/action HyperlinkAction.java,NONE,1.1
Status: Beta
Brought to you by:
netsrak
|
From: <ne...@pr...> - 2004-01-28 01:15:08
|
Update of /cvsroot/kafenio/kafenio_main/src/de/xeinfach/kafenio/action In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26488/src/de/xeinfach/kafenio/action Added Files: HyperlinkAction.java Log Message: added new Hyperlink insertion dialog from maxym (insert link from server is currently not available.) --- NEW FILE: HyperlinkAction.java --- package de.xeinfach.kafenio.action; import de.xeinfach.kafenio.KafenioMenuItem; import de.xeinfach.kafenio.KafenioPanel; import de.xeinfach.kafenio.util.Translatrix; import java.awt.event.ActionEvent; import java.util.Arrays; import java.util.Enumeration; import java.util.Vector; import javax.swing.JComboBox; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.JTextPane; import javax.swing.text.BadLocationException; import javax.swing.text.SimpleAttributeSet; import javax.swing.text.StyledEditorKit; import javax.swing.text.html.HTML; import javax.swing.text.html.HTMLDocument; /** * * @author Maxym Mykhalchuk */ public class HyperlinkAction extends StyledEditorKit.StyledTextAction { private KafenioPanel parentKafenio; /** * Creates a new instance of HyperlinkAction * @param kafenio the parent KafenioPanel object. */ public HyperlinkAction(KafenioPanel kafenio) { super(Translatrix.getTranslationString("InsertAnchor") + KafenioMenuItem.DOTS); this.parentKafenio = kafenio; } /** * The method, which actually does insertion of a link. * @param event the ActionEvent to process. */ public void actionPerformed(ActionEvent event) { JTextPane pane = parentKafenio.getTextPane(); HTMLDocument document = ((HTMLDocument) pane.getDocument()); Object attrs = pane.getCharacterAttributes().getAttribute(HTML.Tag.A); // if the link is not under the caret, and the selection is not empty, // then checking the whole selection int caret = pane.getCaretPosition(); int selStart = pane.getSelectionStart(); int selEnd = pane.getSelectionEnd(); if( ( attrs == null ) && ( selEnd > selStart ) ) { for(int i=selStart; i<selEnd; i++) { pane.setCaretPosition(i); attrs = pane.getCharacterAttributes().getAttribute(HTML.Tag.A); if( attrs != null ) break; } pane.setCaretPosition(caret); pane.select(selStart, selEnd); } String currentLink = ""; String currentTarget = ""; if( (attrs != null) && (attrs instanceof SimpleAttributeSet) ) { SimpleAttributeSet as = (SimpleAttributeSet)attrs; Enumeration names = as.getAttributeNames(); while(names.hasMoreElements()) { Object name = names.nextElement(); if(name.toString().toLowerCase().equals("href")) { currentLink = as.getAttribute(name).toString(); } else if(name.toString().toLowerCase().equals("target")) { currentTarget = as.getAttribute(name).toString(); } } } JTextField text = new JTextField(20); if( selEnd > selStart ) { text.setText(pane.getSelectedText()); } JTextField url = new JTextField(currentLink, 20); Vector targets = new Vector(Arrays.asList(new String[] {"", "_new", "_blank", "_parent", "top"})); if( targets.indexOf(currentTarget) == -1 ) targets.add(0,currentTarget); JComboBox target = new JComboBox(targets); target.setEditable(true); Object[] controls = new Object[] { new JLabel(Translatrix.getTranslationString("InsertAnchorTextField")), text, new JLabel(Translatrix.getTranslationString("InsertAnchorURLField")), url, new JLabel(Translatrix.getTranslationString("InsertAnchorTargetField")), target }; Object[] options; if( attrs != null ) options = new String[] { Translatrix.getTranslationString("InsertAnchorInsertLinkButton"), Translatrix.getTranslationString("DialogCancel"), Translatrix.getTranslationString("InsertAnchorRemoveLinkButton")}; else options = new String[] { Translatrix.getTranslationString("InsertAnchorInsertLinkButton"), Translatrix.getTranslationString("DialogCancel")}; int option = JOptionPane.showOptionDialog( parentKafenio, controls, Translatrix.getTranslationString("AnchorDialogTitle"), JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0] ); SimpleAttributeSet as = new SimpleAttributeSet(pane.getCharacterAttributes().copyAttributes()); switch( option ) { case 0: // insert link // removing previous link as.removeAttribute(HTML.Tag.A); SimpleAttributeSet a = new SimpleAttributeSet(); a.addAttribute(HTML.Attribute.HREF, url.getText()); if( target.getSelectedItem().toString() != "" ) a.addAttribute(HTML.Attribute.TARGET, target.getSelectedItem().toString()); as.addAttribute(HTML.Tag.A, a); // and adding new link try { if( selEnd > selStart ) document.remove(selStart, selEnd-selStart); document.insertString(selStart, text.getText(), as); } catch( BadLocationException ble ) { System.out.println("BadLocationException in insertLink: "+ble.getLocalizedMessage()); } break; case 2: // remove link as.removeAttribute(HTML.Tag.A); if( selEnd > selStart ) document.setCharacterAttributes(selStart, selEnd-selStart, as, true); break; case 1: // cancel case JOptionPane.CLOSED_OPTION: break; } } } |