1 7 8 import java.lang.*; 9 import java.awt.*; 10 import java.awt.event.*; 11 import java.awt.datatransfer.*; 12 import java.util.*; 13 import java.beans.*; 14 import java.net.URL ; 15 import javax.swing.*; 16 import javax.swing.event.*; 17 import javax.swing.text.*; 18 import javax.swing.text.html.*; 19 20 21 34 public class TextEditor extends JApplet{ 35 int cnt=0; 36 37 public static final String _fontfamily = "fontfamily"; 38 39 public static final String _fontsize = "fontsize"; 40 41 public static final String _family = "family"; 42 43 public static final String _size = "size"; 44 45 public static final String _label = "Label"; 46 47 public static final String _action = "Action"; 48 49 public static final String _tooltip = "Tooltip"; 50 51 public static final int _maxComboItems = 20; 52 53 54 private JToolBar toolbar; 55 56 private JMenuBar MenuBar; 57 58 private JComboBox comboFontFamilies; 59 60 private JComboBox comboFontSizes; 61 62 private ActionListener comboFontListener=new ActionListener() { 63 public void actionPerformed(ActionEvent e) { 64 comboFontActions(e); 65 } 66 }; 67 68 private ActionListener toggleButtonListener=new ActionListener() { 69 public void actionPerformed(ActionEvent e) { 70 toggleButtonActions(e); 71 } 72 }; 73 public static final String copyExtAction = "copyExt"; 74 75 private SimpleAttributeSet currentAS; 76 77 private boolean isEnterPressed=false; 78 79 private boolean isBSValid=false; 80 81 private MyTextPane editor; 82 83 private Hashtable commands; 84 85 private static ResourceBundle resources; 86 87 private static ResourceBundle resdef; 88 89 private static ResourceBundle resText; 90 91 String res; 92 93 String resLan; 94 95 private Element[] rootElements; 96 97 private JPopupMenu PopUp; 98 99 private JToggleButton boldButton; 100 101 private JToggleButton italicButton; 102 103 private JToggleButton underButton; 104 105 private JToggleButton leftButton; 106 107 private JToggleButton centerButton; 108 109 private JToggleButton rightButton; 110 111 private ButtonGroup group=new ButtonGroup(); 112 117 private String defLineHeight="18"; 118 122 private String defHyph="en"; 123 128 private String defFontFamily="Serif"; 129 132 private String defFontSize="16"; 133 134 private boolean defbold=false; 135 136 private boolean defitalic=false; 137 138 private boolean defunder=false; 139 151 public String getParameter(String key, String def) { 152 if (getParameter(key) != null) { 153 return getParameter(key); 154 } 155 return def; 156 } 157 158 159 public TextEditor() { 160 } 161 167 public void init() { 168 res = getParameter("Resource", "TextEditor"); 170 resLan=getParameter("Language",""); 171 try { 173 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 174 } 175 catch(Exception e) { 176 e.printStackTrace(); 177 } 178 try { 180 resources = ResourceBundle.getBundle(res); 181 } 182 catch (MissingResourceException MRe) { 183 System.err.println(res+".properties not found"); 184 System.exit(0); 185 } 186 try { 187 resdef=ResourceBundle.getBundle("Text"); 188 } 189 catch (MissingResourceException MRe) { 190 System.err.println("Text.properties not found"); 191 System.exit(0); 192 } 193 try { 194 resText=ResourceBundle.getBundle("Text_"+resLan); 195 } 196 catch (MissingResourceException MRe) { 197 resText=ResourceBundle.getBundle("Text"); 198 System.err.println("Text_"+resLan+".properties not found, using default Text.properties !!!"); 199 } 200 super.init(); 201 try { 202 jbInit(); 203 } 204 catch (Exception e) { 205 e.printStackTrace(); 206 } 207 208 } 209 213 protected void jbInit() throws Exception { 214 setSize(new Dimension(600, 400)); 216 JPanel Glavni = new JPanel(); 218 Glavni.setBorder(BorderFactory.createEtchedBorder()); 219 Glavni.setLayout(new BorderLayout()); 220 editor=createEditor(); 222 editor.addMouseListener(new PopMouseListener()); 223 editor.addCaretListener(new CaretListener(){ 224 public void caretUpdate(CaretEvent e) { 225 caretChange(e); 226 } 227 }); 228 editor.addKeyListener(new KeyAdapter(){ 229 public void keyPressed(KeyEvent e){ 230 int keyCode=e.getKeyCode(); 231 if ((keyCode==KeyEvent.VK_ENTER) || (keyCode==KeyEvent.VK_BACK_SPACE)) { 232 StyledDocument theDocument = getEditor().getStyledDocument(); 233 int dot = getEditor().getCaret().getDot(); 234 Element prgel=theDocument.getParagraphElement(dot); 236 int stof=prgel.getStartOffset(); 238 int enof=prgel.getEndOffset(); 239 240 if (keyCode==KeyEvent.VK_BACK_SPACE && !(dot==stof+1 && enof==stof+2)) { 243 return; 244 } 245 246 AttributeSet cpas=getEditor().getParagraphAttributes(); 248 AttributeSet ccas=editor.getInputAttributes(); 249 250 currentAS=new SimpleAttributeSet(cpas); 251 252 StyleConstants.setFontFamily(currentAS,StyleConstants.getFontFamily(ccas)); 253 StyleConstants.setFontSize(currentAS,StyleConstants.getFontSize(ccas)); 254 StyleConstants.setBold(currentAS,StyleConstants.isBold(ccas)); 255 StyleConstants.setItalic(currentAS,StyleConstants.isItalic(ccas)); 256 StyleConstants.setUnderline(currentAS,StyleConstants.isUnderline(ccas)); 257 258 if (keyCode==KeyEvent.VK_ENTER) { 259 isEnterPressed=true; 260 } 261 if (keyCode==KeyEvent.VK_BACK_SPACE) { 262 isBSValid=true; 263 } 264 265 if ((dot==stof) && (enof-stof==1) && (keyCode==KeyEvent.VK_ENTER)) { 267 editor.setParagraphAttributes(currentAS,true); 268 } 269 } 270 } 271 272 public void keyTyped(KeyEvent e){ 273 int keyCode=e.getKeyChar(); 274 if (keyCode==KeyEvent.VK_BACK_SPACE) { 277 if (isBSValid) { 278 int dot = getEditor().getCaret().getDot(); 279 StyledDocument theDocument = getEditor().getStyledDocument(); 280 Element prgel=theDocument.getParagraphElement(dot); 282 int stof=prgel.getStartOffset(); 284 int enof=prgel.getEndOffset(); 285 if (enof > theDocument.getLength()) { 286 enof=theDocument.getLength(); 287 } 288 editor.moveCaretPosition(enof); 289 editor.setParagraphAttributes(currentAS,true); 290 editor.setCharacterAttributes(currentAS,true); 291 editor.moveCaretPosition(dot); 292 isBSValid=false; 293 } 294 } 295 296 if (keyCode==KeyEvent.VK_ENTER && isEnterPressed==true) { 297 editor.setParagraphAttributes(currentAS,true); 298 editor.setCharacterAttributes(currentAS,true); 299 isEnterPressed=false; 300 } 301 } 302 303 }); 304 commands = new Hashtable(); 306 Action[] actions = getActions(); 307 for (int i = 0; i < actions.length; i++) { 308 Action a = actions[i]; 309 commands.put(a.getValue(Action.NAME), a); 310 } 311 JScrollPane scroll = new JScrollPane(); 312 JViewport port = scroll.getViewport(); 313 port.add(editor); 314 if (getResourceString("menubar")!=null) 315 MenuBar=createMenuBar(); 316 JPanel panel=new JPanel(); 317 Glavni.add(scroll,BorderLayout.CENTER); 318 if (getResourceString("toolbar")!=null) 319 Glavni.add(createToolbar(),BorderLayout.NORTH); 320 this.setJMenuBar(MenuBar); 322 this.getContentPane().add(Glavni); 323 setContentPane(Glavni); 324 editor.requestFocus(); 325 } 326 331 protected MyTextPane createEditor(){ 332 editor=new MyTextPane(); 333 StyleContext sc = new StyleContext(); 335 DefaultStyledDocument doc = new DefaultStyledDocument(sc); 336 editor.setStyledDocument(doc); 337 338 setDefaultFonts(); 340 setDefaultColors(); 341 342 Style def = sc.getStyle(StyleContext.DEFAULT_STYLE); 344 StyleConstants.setFontFamily(def,defFontFamily); 345 StyleConstants.setFontSize(def,Integer.parseInt(defFontSize)); 346 StyleConstants.setBold(def,defbold); 347 StyleConstants.setItalic(def,defitalic); 348 StyleConstants.setUnderline(def,defunder); 349 350 return editor; 351 } 352 357 protected void setDefaultFonts(){ 358 MutableAttributeSet defAtt=editor.getInputAttributes(); 359 try{ 360 String [] defKeys = tokenize(getResourceString("defvalues")); 361 String [] keyval=new String [defKeys.length]; 362 for (int i = 0; i < defKeys.length; i++) { 363 keyval[i]=getResourceString(defKeys[i]); 364 if (defKeys[i].equals("defFontFamily")){ 365 String ff=keyval[i].toString(); 366 if (validateDefaults(_family,ff)) 367 defFontFamily=ff; 368 } 369 else if (defKeys[i].equals("defFontSize")){ 370 String fs=keyval[i].toString(); 371 if (validateDefaults(_size,fs)) 372 defFontSize=fs; 373 } 374 else if (defKeys[i].equals("defbold")){ 375 String bld=keyval[i].toString(); 376 if (bld.equals("true")) 377 defbold=true; 378 } 379 else if (defKeys[i].equals("defitalic")){ 380 String itl=keyval[i].toString(); 381 if (itl.equals("true")) 382 defitalic=true; 383 } 384 else if (defKeys[i].equals("defunder")){ 385 String und=keyval[i].toString(); 386 if (und.equals("true")) 387 defunder=true; 388 } 389 } 390 } 391 catch(Exception e){ 392 System.err.println("There is no default values for editor in property file"); 393 } 394 int defFSi=Integer.parseInt(defFontSize); 395 StyleConstants.setFontFamily(defAtt,defFontFamily); 397 StyleConstants.setFontSize(defAtt, defFSi); 399 StyleConstants.setBold(defAtt,defbold); 401 StyleConstants.setItalic(defAtt,defitalic); 402 StyleConstants.setUnderline(defAtt,defunder); 403 String strLH=getResourceString("lineHeight"); 405 int intLH; 406 try { 408 intLH=Integer.parseInt(strLH); 409 if (intLH>=8 && intLH<=96) { 410 defLineHeight=strLH; 411 } 412 else { 413 if (intLH<8) defLineHeight="8"; 414 else defLineHeight="96"; 415 System.err.println("Warning, line height value should be beetwen 8 and 96, using value of "+defLineHeight); 416 } 417 } 418 catch (Exception e) { 419 System.err.println("Error reading lineHeight parameter, using predefined value of "+defLineHeight); 420 } 421 String strHyph=getResourceString("defHyphen"); 423 if (strHyph==null) 424 System.out.println("Error while reading defHyphen parameter, using predefined value: "+defHyph); 425 else 426 defHyph=strHyph; 427 } 428 444 private boolean validateDefaults (String key,String value) { 445 int i = 1; 448 Vector vecSizesOrFamilies=new Vector(); 449 int substrstart; 450 if (key.equals(_size)) substrstart=10; 451 else substrstart=12; 452 for( String action; null !=( action = getResourceString( key + i + _action )); ++i ) { 453 action=action.substring(substrstart); 456 vecSizesOrFamilies.addElement(action); 457 } 458 if (vecSizesOrFamilies.contains(value)) { 460 return true; 461 } 462 else { 463 if (key.equals(_size)) { 466 if(!(vecSizesOrFamilies.contains(defFontSize)) && 467 !(vecSizesOrFamilies.isEmpty())) 468 defFontSize=(String )vecSizesOrFamilies.firstElement(); 469 } 470 else { 471 if(!(vecSizesOrFamilies.contains(defFontFamily)) && 472 !(vecSizesOrFamilies.isEmpty())) 473 defFontFamily=(String )vecSizesOrFamilies.firstElement(); 474 } 475 return false; 476 } 477 } 478 483 protected void setDefaultColors(){ 484 485 Color foreground=Color.green; 486 Color background=Color.black; 487 Color caretColor=Color.yellow; 488 Color selectedTextColor=Color.yellow; 489 Color selectionColor=Color.lightGray; 490 Color border=SystemColor.info; 491 498 499 String strCol[]={"Color.black","Color.blue","Color.cyan","Color.darkGray","Color.gray", 500 "Color.green","Color.lightGray","Color.magenta","Color.orange", 501 "Color.pink","Color.red","Color.white","Color.yellow", 502 "SystemColor.desktop","SystemColor.activeCaption", 503 "SystemColor.activeCaptionText","SystemColor.activeCaptionBorder", 504 "SystemColor.inactiveCaption","SystemColor.inactiveCaptionText", 505 "SystemColor.inactiveCaptionBorder","SystemColor.window", 506 "SystemColor.windowBorder","SystemColor.windowText", 507 "SystemColor.menu","SystemColor.menuText","SystemColor.text", 508 "SystemColor.textText","SystemColor.textHighlight", 509 "SystemColor.textHighlightText","SystemColor.textInactiveText", 510 "SystemColor.control","SystemColor.controlText", 511 "SystemColor.controlHighlight","SystemColor.controlLtHighlight", 512 "SystemColor.controlShadow","SystemColor.controlDkShadow", 513 "SystemColor.scrollbar","SystemColor.info","SystemColor.infoText"}; 514 515 Color colCol[]={Color.black,Color.blue,Color.cyan,Color.darkGray,Color.gray, 516 Color.green,Color.lightGray,Color.magenta,Color.orange, 517 Color.pink,Color.red,Color.white,Color.yellow, 518 SystemColor.desktop,SystemColor.activeCaption, 519 SystemColor.activeCaptionText,SystemColor.activeCaptionBorder, 520 SystemColor.inactiveCaption,SystemColor.inactiveCaptionText, 521 SystemColor.inactiveCaptionBorder,SystemColor.window, 522 SystemColor.windowBorder,SystemColor.windowText, 523 SystemColor.menu,SystemColor.menuText,SystemColor.text, 524 SystemColor.textText,SystemColor.textHighlight, 525 SystemColor.textHighlightText,SystemColor.textInactiveText, 526 SystemColor.control,SystemColor.controlText, 527 SystemColor.controlHighlight,SystemColor.controlLtHighlight, 528 SystemColor.controlShadow,SystemColor.controlDkShadow, 529 SystemColor.scrollbar,SystemColor.info,SystemColor.infoText}; 530 Vector vStrCol=new Vector(); 532 Vector vColCol=new Vector(); 533 for (int i=0; i<strCol.length;i++) { 534 vStrCol.addElement(strCol[i]); 535 vColCol.addElement(colCol[i]); 536 } 537 try{ 539 String [] defKeys = tokenize(getResourceString("defvalues")); 540 String [] keyval=new String [defKeys.length]; 541 for (int i = 0; i < defKeys.length; i++) { 542 keyval[i]=getResourceString(defKeys[i]); 543 if (defKeys[i].equals("foreground")){ 544 foreground=getColor(keyval[i],foreground,vStrCol,vColCol); 545 } 546 else if (defKeys[i].equals("background")){ 547 background=getColor(keyval[i],background,vStrCol,vColCol); 548 } 549 else if (defKeys[i].equals("caretColor")){ 550 caretColor=getColor(keyval[i],caretColor,vStrCol,vColCol); 551 } 552 else if (defKeys[i].equals("selectedTextColor")){ 553 selectedTextColor=getColor(keyval[i],selectedTextColor,vStrCol,vColCol); 554 } 555 else if (defKeys[i].equals("selectionColor")){ 556 selectionColor=getColor(keyval[i],selectionColor,vStrCol,vColCol); 557 } 558 else if (defKeys[i].equals("border")){ 559 border=getColor(keyval[i],border,vStrCol,vColCol); 560 } 561 } 562 } 563 catch(Exception e){ 564 System.err.println("There is no default values for editor in property file"); 565 } 566 editor.setForeground(foreground); 568 editor.setBackground(background); 570 editor.setCaretColor(caretColor); 572 editor.setSelectedTextColor(selectedTextColor); 574 editor.setSelectionColor(selectionColor); 576 editor.setBorder(BorderFactory.createLineBorder(border,1)); 578 } 579 590 private Color getColor(String strColor,Color defColor,Vector vStrCol,Vector vColCol) { 591 Color colorVal=defColor; 592 if (vStrCol.contains(strColor)) { 593 int ind=vStrCol.indexOf(strColor); 594 colorVal=(Color)vColCol.elementAt(ind); 595 } 596 return colorVal; 597 } 598 604 protected JMenuBar createMenuBar(){ 605 JMenuItem mi; 606 JMenuBar mb = new JMenuBar(); 607 String [] menuKeys = tokenize(getResourceString("menubar")); 608 for (int i = 0; i < menuKeys.length; i++) { 609 JMenu m = createMenu(menuKeys[i]); 610 if (m != null) 611 mb.add(m); 612 } 613 return mb; 614 } 615 622 protected JMenu createMenu(String key) { 623 String [] itemKeys = tokenize(getResourceString(key)); 624 JMenu menu = new JMenu(getResourceString(key + "Label")); 625 menu.setMnemonic(menu.getText().charAt(0)); 626 for (int i = 0; i < itemKeys.length; i++) { 627 if (itemKeys[i].equals("-")) { 628 menu.addSeparator(); 629 } 630 else { 631 JMenuItem mi = createMenuItem(itemKeys[i]); 632 menu.add(mi); 633 } 634 } 635 return menu; 636 } 637 644 protected JMenuItem createMenuItem(String cmd) { 645 JMenuItem mi = new JMenuItem(getResourceString(cmd + "Label")); 646 if (cmd.equals("about")){ 647 mi.addActionListener(new ActionListener(){ 649 public void actionPerformed(ActionEvent e){ 650 helpAbout(e); 651 } 652 }); 653 } 654 else if (cmd.equals("parametar")){ 655 mi.addActionListener(new ActionListener(){ 657 public void actionPerformed(ActionEvent e){ 658 helpParam(e); 659 } 660 }); 661 } 662 else{ 663 URL url = getResource(cmd + "ImageMenu"); 664 if (url != null) { 665 mi.setHorizontalTextPosition(JButton.RIGHT); 666 mi.setIcon(new ImageIcon(url)); 667 } 668 String astr = getResourceString(cmd + "Action"); 669 if (astr == null) astr = cmd; 670 final String astrf=astr; 671 final String cmdf=cmd; 672 mi.setActionCommand(astrf); 673 final Action a = getAction(astrf); 674 if (a != null) { 675 if (astrf.substring(0,4).equals("font")) { 677 if (cmdf.equals("bold")) { 678 mi.addActionListener(new ActionListener() { 679 public void actionPerformed(ActionEvent ae) { 680 a.actionPerformed(ae); 681 if (boldButton!=null) { 682 boldButton.setSelected(!boldButton.isSelected()); 683 } 684 } 685 }); 686 } 687 else if (cmdf.equals("italic")) { 688 mi.addActionListener(new ActionListener() { 689 public void actionPerformed(ActionEvent ae) { 690 a.actionPerformed(ae); 691 if (italicButton!=null) { 692 italicButton.setSelected(!italicButton.isSelected()); 693 } 694 } 695 }); 696 } 697 else if (cmdf.equals("underline")) { 698 mi.addActionListener(new ActionListener() { 699 public void actionPerformed(ActionEvent ae) { 700 a.actionPerformed(ae); 701 if (underButton!=null) { 702 underButton.setSelected(!underButton.isSelected()); 703 } 704 } 705 }); 706 } 707 else if (astrf.substring(5,9).equals(_size)) { 708 mi.addActionListener(new ActionListener() { 709 public void actionPerformed(ActionEvent ae) { 710 a.actionPerformed(ae); 711 if (comboFontSizes!=null) { 712 comboFontSizes.removeActionListener(comboFontListener); 713 comboFontSizes.setSelectedItem(getResourceString(cmdf + _label)); 714 comboFontSizes.addActionListener(comboFontListener); 715 } 716 } 717 }); 718 } 719 else if (astrf.substring(5,11).equals(_family)) { 720 mi.addActionListener(new ActionListener() { 721 public void actionPerformed(ActionEvent ae) { 722 a.actionPerformed(ae); 723 if (comboFontFamilies!=null) { 724 comboFontFamilies.removeActionListener(comboFontListener); 725 comboFontFamilies.setSelectedItem(getResourceString(cmdf + _label)); 726 comboFontFamilies.addActionListener(comboFontListener); 727 } 728 } 729 }); 730 } 731 } 732 else { 733 mi.addActionListener(a); 734 } 735 mi.setEnabled(a.isEnabled()); 736 } 737 else mi.setEnabled(false); 738 } 739 return mi; 740 } 741 753 protected String getResourceString(String nm) { 754 String str; 755 try { 756 str = this.resources.getString(nm); 757 } 758 catch (MissingResourceException mre) { 759 try { 760 str = this.resText.getString(nm); 761 } 762 catch (MissingResourceException mree) { 763 try { 764 str = this.resdef.getString(nm); 765 } 766 catch (MissingResourceException mreee) { 767 str = null; 768 } 769 } 770 } 771 if (str != null) str=str.trim(); 772 return str; 773 } 774 780 protected URL getResource(String key) { 781 String name = getResourceString(key); 782 if (name != null) { 783 URL url = this.getClass().getResource(name); 784 return url; 785 } 786 return null; 787 } 788 794 public static String [] tokenize(String input) { 795 Vector v = new Vector(); 796 StringTokenizer t = new StringTokenizer(input); 797 String cmd[]; 798 while (t.hasMoreTokens()) 799 v.addElement(t.nextToken()); 800 cmd = new String [v.size()]; 801 for (int i = 0; i < cmd.length; i++) 802 cmd[i] = (String ) v.elementAt(i); 803 return cmd; 804 } 805 809 public Action[] getActions() { 810 Action[] defaultActions = { 811 new CopyExtAction()}; 812 Action[] a = TextAction.augmentList(editor.getActions(), defaultActions); 813 824 return a; 826 } 827 832 protected Action getAction(String cmd) { 833 return (Action) commands.get(cmd); 834 } 835 839 public JTextPane getEditor() { 840 return editor; 841 } 842 848 protected Component createToolbar() { 849 toolbar = new JToolBar(); 850 String [] toolKeys = tokenize(getResourceString("toolbar")); 851 for (int i = 0; i < toolKeys.length; i++) { 852 if (toolKeys[i].equals("-")) { 853 toolbar.add(Box.createHorizontalStrut(10)); 854 } 855 else { 856 toolbar.add(createTool(toolKeys[i])); 857 } 858 } 859 toolbar.add(Box.createHorizontalGlue()); 860 JButton but=new JButton("Dump"); 861 but.addActionListener(new ActionListener() { 862 public void actionPerformed(ActionEvent e) { 863 testing(e); 864 } 865 }); 866 867 869 leftButton.setSelected(true); 870 return toolbar; 871 } 872 879 protected Component createTool(String key) { 880 if (key.equals("fontfamily") || key.equals("fontsize")) 881 return createComboBoxes(key); 882 else if(key.equals("bold") || key.equals("italic") || key.equals("underline")){ 883 return createToggleButton(key); 884 } 885 else if(key.equals("left") || key.equals("center") || key.equals("right")){ 886 return createToggleGroup(key); 887 } 888 else 889 return createToolbarButton(key); 890 } 891 899 protected JToggleButton createToggleGroup(String key){ 900 JToggleButton Tb = everyToggle(key); 901 Tb.setRequestFocusEnabled(false); 902 Tb.setMargin(new Insets(1,1,1,1)); 903 String astr = getResourceString(key + "Action"); 904 if (astr == null) { 905 astr = key; 906 } 907 Action a = getAction(astr); 908 if (a != null) { 909 Tb.setActionCommand(astr); 910 Tb.addActionListener(a); 911 } 912 else { 913 Tb.setEnabled(false); 914 } 915 String tip = getResourceString(key + "Tooltip"); 916 if (tip != null) { 917 Tb.setToolTipText(tip); 918 } 919 group.add(Tb); 920 return Tb; 921 } 922 927 private void helpAbout(ActionEvent e){ 928 JOptionPane.showMessageDialog(this, new InfoPanel(), getResourceString("aboutLabel"), JOptionPane.INFORMATION_MESSAGE); 929 } 930 935 private void helpParam(ActionEvent e){ 936 JOptionPane.showMessageDialog(this, new InfoPanel(resources,resLan,res,defHyph), getResourceString("parametarLabel"), JOptionPane.INFORMATION_MESSAGE); 937 } 938 944 protected JToggleButton everyToggle(String name){ 945 URL url = getResource(name + "ImageTool"); 946 if (name.equals("bold")){ 947 try { 948 boldButton=new JToggleButton(new ImageIcon(url),defbold){ 949 public float getAlignmentY() { return 0.5f; } 950 }; 951 } 952 catch (Exception e) { 953 boldButton=new JToggleButton(name,defbold){ 954 public float getAlignmentY() { return 0.5f; } 955 }; 956 } 957 return boldButton; 958 } 959 else if (name.equals("italic")){ 960 try { 961 italicButton=new JToggleButton(new ImageIcon(url),defitalic){ 962 public float getAlignmentY() { return 0.5f; } 963 }; 964 } 965 catch (Exception e) { 966 italicButton=new JToggleButton(name,defitalic){ 967 public float getAlignmentY() { return 0.5f; } 968 }; 969 } 970 return italicButton; 971 } 972 else if(name.equals("underline")){ 973 try { 974 underButton=new JToggleButton(new ImageIcon(url),defunder){ 975 public float getAlignmentY() { return 0.5f; } 976 }; 977 } 978 catch (Exception e) { 979 underButton=new JToggleButton(name,defunder){ 980 public float getAlignmentY() { return 0.5f; } 981 }; 982 } 983 return underButton; 984 } 985 else if(name.equals("left")){ 986 try { 987 leftButton=new JToggleButton(new ImageIcon(url),false){ 988 public float getAlignmentY() { return 0.5f; } 989 }; 990 } 991 catch (Exception e) { 992 leftButton=new JToggleButton(name,false){ 993 public float getAlignmentY() { return 0.5f; } 994 }; 995 } 996 return leftButton; 997 } 998 else if(name.equals("center")){ 999 try { 1000 centerButton=new JToggleButton(new ImageIcon(url),false){ 1001 public float getAlignmentY() { return 0.5f; } 1002 }; 1003 } 1004 catch (Exception e) { 1005 centerButton=new JToggleButton(name,false){ 1006 public float getAlignmentY() { return 0.5f; } 1007 }; 1008 } 1009 return centerButton; 1010 } 1011 else { 1012 try { 1013 rightButton=new JToggleButton(new ImageIcon(url),false){ 1014 public float getAlignmentY() { return 0.5f; } 1015 }; 1016 } 1017 catch (Exception e) { 1018 rightButton=new JToggleButton(name,false){ 1019 public float getAlignmentY() { return 0.5f; } 1020 }; 1021 } 1022 return rightButton; 1023 } 1024 } 1025 1032 protected JToggleButton createToggleButton(String key){ 1033 JToggleButton Tb = everyToggle(key); 1034 Tb.setRequestFocusEnabled(false); 1035 Tb.setMargin(new Insets(1,1,1,1)); 1036 String astr = getResourceString(key + "Action"); 1037 if (astr == null) { 1038 astr = key; 1039 } 1040 Action a = getAction(astr); 1041 if (a != null) { 1042 Tb.setActionCommand(astr); 1043 Tb.addActionListener(toggleButtonListener); 1044 } 1045 else { 1046 Tb.setEnabled(false); 1047 } 1048 String tip = getResourceString(key + "Tooltip"); 1049 if (tip != null) { 1050 Tb.setToolTipText(tip); 1051 } 1052 return Tb; 1053 } 1054 1057 1066 protected JComboBox createComboBoxes(String key){ 1067 key =( key.equals( _fontfamily ))?( _family ):( _size ); 1069 int i = 1; 1071 String [] tmp = new String [ _maxComboItems ]; 1072 for( String label; null !=( label = getResourceString( key + i + _label )); ++i ) 1073 tmp[ i ]= label; 1074 String [] itemlist = new String [ i ]; 1077 itemlist[ i - 1 ]= ""; 1078 while( --i > 0 ) 1080 itemlist[ i - 1 ]= tmp[ i ]; 1081 JComboBox cb = new JComboBox( itemlist ); 1083 1084 String action; 1089 String selection; 1090 String defFamilyOrStyle; 1091 int substrstart; 1092 if (key.equals(_family)) { 1093 defFamilyOrStyle=defFontFamily; 1094 substrstart=12; 1095 } 1096 else { 1097 defFamilyOrStyle=defFontSize; 1098 substrstart=10; 1099 } 1100 for( i = 1; null !=( action = getResourceString( key + i + _action )); ++i ){ 1102 action=action.substring(substrstart); 1104 if( defFamilyOrStyle.equals( action )){ 1105 selection = getResourceString( key + i + _label ); 1107 cb.setSelectedItem(selection); 1108 break; 1109 } 1110 } 1111 1113 cb.addActionListener( comboFontListener ); 1114 tmp[ 1 ]= getResourceString( key + _tooltip ); 1115 if( tmp[ 1 ]!= null ) 1116 cb.setToolTipText( tmp[ 1 ]); 1117 if( key.equals( _family )) 1119 return( comboFontFamilies = cb ); 1120 else 1121 return( comboFontSizes = cb ); 1122 } 1123 1128 protected void comboFontActions(ActionEvent e){ 1129 JComboBox source =( JComboBox )e.getSource(); 1131 String selectedItem = source.getSelectedItem().toString(); 1132 String key =( comboFontFamilies.equals( source ))?( _family ):( _size ); 1133 Action act; 1134 String label; 1135 for( int i = 1; null !=( label = getResourceString( key + i + _label )); ++i ){ 1137 if( selectedItem.equals( label )){ 1138 act = getAction( getResourceString( key + i + _action )); 1140 act.actionPerformed( e ); 1141 editor.requestFocus(); 1143 } 1144 } 1145 } 1146 1151 protected JButton createToolbarButton(String key) { 1152 URL url = getResource(key + "ImageTool"); 1153 JButton b; 1154 try { 1155 b = new JButton(new ImageIcon(url)) { 1156 public float getAlignmentY() { return 0.5f; } 1157 }; 1158 } 1159 catch (Exception e) { 1160 b = new JButton(key) { 1161 public float getAlignmentY() { return 0.5f; } 1162 }; 1163 } 1164 b.setRequestFocusEnabled(false); 1165 b.setMargin(new Insets(1,1,1,1)); 1166 String astr = getResourceString(key + "Action"); 1167 if (astr == null) { 1168 astr = key; 1169 } 1170 Action a = getAction(astr); 1171 if (a != null) { 1172 b.setActionCommand(astr); 1173 b.addActionListener(a); 1174 } 1175 else { 1176 b.setEnabled(false); 1177 } 1178 String tip = getResourceString(key + "Tooltip"); 1179 if (tip != null) { 1180 b.setToolTipText(tip); 1181 } 1182 return b; 1183 } 1184 1189 protected void toggleButtonActions(ActionEvent e) { 1190 JToggleButton source=(JToggleButton)e.getSource(); 1192 String key; 1193 if (source.equals(boldButton)) 1194 key="bold"; 1195 else if (source.equals(italicButton)) 1196 key="italic"; 1197 else 1198 key="underline"; 1199 String astr = getResourceString(key + "Action"); 1201 if (astr == null) { 1202 astr = key; 1203 } 1204 Action a = getAction(astr); 1206 if (a != null) { 1207 a.actionPerformed(e); 1208 } 1209 editor.requestFocus(); 1211 } 1212 1216 public void testing(ActionEvent e){ 1217 java.util.List listDoc=loading(); 1218 Iterator iterDoc=listDoc.iterator(); 1219 while(iterDoc.hasNext()) { 1220 Object nextDoc = iterDoc.next(); 1221 java.util.List listParag=(java.util.List )nextDoc; 1222 Iterator iterParag=listParag.iterator(); 1223 while(iterParag.hasNext()){ 1224 Object nextAlign=iterParag.next(); 1225 int align=Integer.parseInt(nextAlign.toString()); 1226 Object nextSadrzaj=iterParag.next(); 1227 System.out.println("Paragraf: Alignment="+align); 1228 java.util.List listSad=(java.util.List )nextSadrzaj; 1229 Iterator iterSad=listSad.iterator(); 1230 while(iterSad.hasNext()){ 1231 Object nextSad=iterSad.next(); 1232 java.util.List listCon=(java.util.List )nextSad; 1233 Iterator iterCon=listCon.iterator(); 1234 while(iterCon.hasNext()){ 1235 String Text=new String (iterCon.next().toString()); 1236 String FontFam=new String (iterCon.next().toString()); 1237 int FontSize=Integer.parseInt(iterCon.next().toString()); 1238 String bold=new String (iterCon.next().toString()); 1239 String italic=new String (iterCon.next().toString()); 1240 String underline=new String (iterCon.next().toString()); 1241 int Start=Integer.parseInt(iterCon.next().toString()); 1242 int End=Integer.parseInt(iterCon.next().toString()); 1243 System.out.println(" text: "+Text+"["+Start+","+End+"] "+FontFam+" "+FontSize+" "+"Bold="+bold+", Italic="+italic+", Underline="+underline); 1244 } 1245 } 1246 } 1247 } 1248 } 1249 1257 public java.util.List loading(){ 1258 StyledDocument document = getEditor().getStyledDocument(); 1259 rootElements = document.getRootElements(); 1260 Element sect=rootElements[0]; 1262 int sectCnt=sect.getElementCount(); 1263 Element parag=null; 1265 java.util.List listDoc = new ArrayList(); 1266 for (int i=0;i<sectCnt;i++){ 1268 java.util.List listPrg = new ArrayList(); 1269 parag=sect.getElement(i); 1271 int paragCnt=parag.getElementCount(); 1272 if (i==sectCnt-1 && sectCnt>1) paragCnt--; 1273 AttributeSet ParAS = parag.getAttributes(); 1274 int intAlign=StyleConstants.getAlignment(ParAS); 1275 Element content=null; 1276 java.util.List listSad = new ArrayList(); 1277 for(int j=0;j<paragCnt;j++){ 1279 java.util.List listCont = new ArrayList(); 1280 content=parag.getElement(j); 1281 AttributeSet ParASL = content.getAttributes(); 1282 String text; 1283 try{ 1284 text=document.getText(content.getStartOffset(),content.getEndOffset()-content.getStartOffset()); 1285 String chngText=""; 1286 for (int k=0; k<text.length(); k++) { 1287 if (text.charAt(k)=='&') 1288 chngText=chngText+"&"; 1289 else if (text.charAt(k)=='<') 1290 chngText=chngText+"<"; 1291 else if (text.charAt(k)=='>') 1292 chngText=chngText+">"; 1293 else 1294 chngText=chngText+text.charAt(k); 1295 } 1296 text=chngText; 1297 listCont.add(text); 1298 } 1299 catch(BadLocationException e){ 1300 listCont.add(new String ()); 1301 System.err.println("Exception "+e+" in range of given part of document"); 1302 } 1303 listCont.add(new String (StyleConstants.getFontFamily(ParASL))); 1304 listCont.add(new Integer (StyleConstants.getFontSize(ParASL))); 1305 listCont.add(new Boolean (StyleConstants.isBold(ParASL))); 1306 listCont.add(new Boolean (StyleConstants.isItalic(ParASL))); 1307 listCont.add(new Boolean (StyleConstants.isUnderline(ParASL))); 1308 listCont.add(new Integer (content.getStartOffset())); 1309 listCont.add(new Integer (content.getEndOffset())); 1310 listSad.add(listCont); 1312 } 1313 listPrg.add(new Integer (intAlign)); 1316 listPrg.add(listSad); 1317 listDoc.add(listPrg); 1319 } 1320 return listDoc; 1321 } 1322 1327 public String FormatFO(){ 1328 String fo=new String (); 1329 String FOalign="start"; 1330 String FObold=""; 1331 String FOunder=""; 1332 String FOital=""; 1333 String Text=" "; 1334 String FontFam="sans-serif"; 1335 int FontSize=12; 1336 java.util.List listDoc=loading(); 1337 Iterator iterDoc=listDoc.iterator(); 1338 while(iterDoc.hasNext()) { 1339 Object nextDoc = iterDoc.next(); 1340 java.util.List listParag=(java.util.List )nextDoc; 1341 Iterator iterParag=listParag.iterator(); 1342 while(iterParag.hasNext()){ 1343 Object nextAlign=iterParag.next(); 1344 int align=Integer.parseInt(nextAlign.toString()); 1345 if (align==0) 1346 FOalign="start"; 1347 else if(align==1) 1348 FOalign="center"; 1349 else if(align==2) 1350 FOalign="end"; 1351 Object nextSadrzaj=iterParag.next(); 1352 fo+="<fo:block font-size=\"12pt\" font-family=\"sans-serif\" line-height=\""+defLineHeight+"pt\" space-after.optimum=\"3pt\" text-align=\""+FOalign+"\" white-space-collapse=\"false\" language=\""+defHyph+"\" hyphenate=\"true\">"; 1353 java.util.List listSad=(java.util.List )nextSadrzaj; 1354 Iterator iterSad=listSad.iterator(); 1355 while(iterSad.hasNext()){ 1356 Object nextSad=iterSad.next(); 1357 java.util.List listCon=(java.util.List )nextSad; 1358 Iterator iterCon=listCon.iterator(); 1359 while(iterCon.hasNext()){ 1360 Text=new String (iterCon.next().toString()); 1361 if (Text.equals("\n")){ 1362 Text=" "; 1363 } 1364 FontFam=new String (iterCon.next().toString()); 1365 FontSize=Integer.parseInt(iterCon.next().toString()); 1366 String bold=new String (iterCon.next().toString()); 1367 String italic=new String (iterCon.next().toString()); 1368 String underline=new String (iterCon.next().toString()); 1369 int Start=Integer.parseInt(iterCon.next().toString()); 1370 int End=Integer.parseInt(iterCon.next().toString()); 1371 if (FontFam.equals("Monospaced")) 1372 FontFam="Courier"; 1373 else if (FontFam.equals("SansSerif")) 1374 FontFam="Helvetica"; 1375 else 1376 FontFam="Times Roman"; 1377 if (bold.equals("true")) 1378 FObold="font-weight=\"bold\" "; 1379 else 1380 FObold=""; 1381 if (italic.equals("true")) 1382 FOital="font-style=\"italic\" "; 1383 else 1384 FOital=""; 1385 if (underline.equals("true")) 1386 FOunder="text-decoration=\"underline\" "; 1387 else 1388 FOunder=""; 1389 fo+="<fo:inline font-family=\""+FontFam+"\" font-size=\""+FontSize+"pt\" "+FObold+FOital+FOunder+">"+Text; 1390 fo+="</fo:inline>"; 1391 } 1392 } 1393 fo+="</fo:block>"; 1394 } 1395 } 1396 return fo; 1397 } 1398 1405 protected void caretChange(CaretEvent e){ 1406 if (isEnterPressed==true) { 1408 return; 1409 } 1410 if (isBSValid==true) { 1411 return; 1412 } 1413 int dot = e.getDot(); 1415 int mark = e.getMark(); 1416 if(dot != mark){ 1417 if (comboFontFamilies != null) { 1418 comboFontFamilies.removeActionListener(comboFontListener); 1419 comboFontFamilies.setSelectedItem(""); 1420 comboFontFamilies.addActionListener(comboFontListener); 1421 } 1422 if (comboFontSizes != null) { 1423 comboFontSizes.removeActionListener(comboFontListener); 1424 comboFontSizes.setSelectedItem(""); 1425 comboFontSizes.addActionListener(comboFontListener); 1426 } 1427 return; 1428 } 1429 1430 StyledDocument theDocument = getEditor().getStyledDocument(); 1431 Element prgel=theDocument.getParagraphElement(dot); 1433 Element leftel=theDocument.getCharacterElement(dot-1); 1434 Element rightel=theDocument.getCharacterElement(dot); 1435 Element curel; 1436 1437 int stof=prgel.getStartOffset(); 1439 int enof=prgel.getEndOffset(); 1440 1441 if (((dot==stof) && ((enof-stof)>1))){ 1443 curel=rightel; 1444 } 1445 else { 1446 curel=leftel; 1447 } 1448 1449 AttributeSet AttChar; 1451 1452 if ((dot==stof) && ((enof-stof)==1)) { 1453 AttChar=editor.getParagraphAttributes(); 1454 } 1455 else { 1456 AttChar=curel.getAttributes(); 1457 } 1458 1459 String ff=StyleConstants.getFontFamily(AttChar); 1460 int fs=StyleConstants.getFontSize(AttChar); 1461 boolean b=StyleConstants.isBold(AttChar); 1462 boolean i=StyleConstants.isItalic(AttChar); 1463 boolean u=StyleConstants.isUnderline(AttChar); 1464 if ((boldButton != null) && (boldButton.isSelected()!=b)) { 1466 boldButton.removeActionListener(toggleButtonListener); 1467 boldButton.setSelected(b); 1468 boldButton.addActionListener(toggleButtonListener); 1469 } 1470 if ((italicButton != null) && (italicButton.isSelected()!=i)) { 1471 italicButton.removeActionListener(toggleButtonListener); 1472 italicButton.setSelected(i); 1473 italicButton.addActionListener(toggleButtonListener); 1474 } 1475 if ((underButton != null) && (underButton.isSelected()!=u)) { 1476 underButton.removeActionListener(toggleButtonListener); 1477 underButton.setSelected(u); 1478 underButton.addActionListener(toggleButtonListener); 1479 } 1480 String key; 1482 String action; 1483 String selection; 1484 if (comboFontFamilies != null) { 1485 comboFontFamilies.removeActionListener(comboFontListener); 1486 key = _family; 1487 for( int j = 1; null !=( action = getResourceString( key + j + _action )); ++j ){ 1489 action=action.substring(12); 1491 if( ff.equals( action )){ 1492 selection = getResourceString( key + j + _label ); 1494 comboFontFamilies.setSelectedItem(selection); 1495 break; 1496 } 1497 } 1498 comboFontFamilies.addActionListener(comboFontListener); 1499 } 1500 if (comboFontSizes != null) { 1501 comboFontSizes.removeActionListener(comboFontListener); 1502 key = _size; 1503 for( int j = 1; null !=( action = getResourceString( key + j + _action )); ++j ){ 1505 action=action.substring(10); 1507 if( String.valueOf(fs).equals( action )){ 1508 selection = getResourceString( key + j + _label ); 1510 comboFontSizes.setSelectedItem(selection); 1511 break; 1512 } 1513 } 1514 comboFontSizes.addActionListener(comboFontListener); 1515 } 1516 AttributeSet AttPar=getEditor().getParagraphAttributes(); 1518 int intAlign=StyleConstants.getAlignment(AttPar); 1520 switch (intAlign){ 1521 case 0: 1522 if ((leftButton != null) && (leftButton.isSelected()!=true)) 1523 leftButton.setSelected(true); 1524 break; 1525 case 1: 1526 if ((centerButton != null) && (centerButton.isSelected()!=true)) 1527 centerButton.setSelected(true); 1528 break; 1529 case 2: 1530 if ((rightButton != null) && (rightButton.isSelected()!=true)) 1531 rightButton.setSelected(true); 1532 break; 1533 } 1534 } 1535 1536 private final class PopMouseListener extends MouseAdapter { 1537 1538 PopMouseListener() { 1539 super(); 1540 } 1541 1542 public void mousePressed(MouseEvent e) { 1543 if ( (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0 ) return; 1544 if (PopUp ==null) buildPop(); 1545 PopUp.show(editor,e.getX(),e.getY()); 1546 } 1547 1548 private void buildPop() { 1549 PopUp = new JPopupMenu(); 1550 String [] itemKeys = {"cut","copy","copyExt","paste"}; 1551 for (int i = 0; i < itemKeys.length; i++) { 1552 switch (itemKeys[i].charAt(0)) { 1553 case '-' : 1554 PopUp.addSeparator(); break; 1555 default: 1556 JMenuItem mi = createMenuItem(itemKeys[i]); 1557 PopUp.add(mi); 1558 } 1559 } 1560 } 1561 } 1562 1563 class CopyExtAction extends AbstractAction{ 1564 1565 public CopyExtAction() { 1566 super(copyExtAction); 1567 } 1568 1569 1572 public void actionPerformed(ActionEvent e) { 1573 editor.copyExt(); 1575 } 1576 1577 } 1578} 1579 1580 | Popular Tags |