public void setSelectionAttribute(Object key, Object value) {
   StyledDocument sd = (StyledDocument) editor.getDocument();
   int a = editor.getSelectionStart();
   int b = editor.getSelectionEnd();
   if (a == b) {
     rtf.getInputAttributes().addAttribute(key, value);
     return;
   }
   SimpleAttributeSet sas = new SimpleAttributeSet();
   sas.addAttribute(key, value);
   sd.setCharacterAttributes(a, b - a, sas, false);
 }
 public void commitChanges() {
   res.put(PGameInformation.BACKGROUND_COLOR, editor.getBackground());
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try {
     StyledDocument doc = (StyledDocument) editor.getDocument();
     rtf.write(baos, doc, doc.getStartPosition().getOffset(), doc.getLength());
     res.put(PGameInformation.TEXT, baos.toString("UTF-8")); // $NON-NLS-1$
   } catch (IOException e) { // Nevermind
     e.printStackTrace();
   } catch (BadLocationException e) { // Should never happen, but we have to catch this anyways
     e.printStackTrace();
   }
 }
 public void saveToFile() {
   fc.setDialogTitle(Messages.getString("GameInformationFrame.SAVE_TITLE")); // $NON-NLS-1$
   if (fc.showSaveDialog(this) != JFileChooser.APPROVE_OPTION) return;
   String name = fc.getSelectedFile().getPath();
   if (CustomFileFilter.getExtension(name) == null) name += ".rtf"; // $NON-NLS-1$
   try {
     FileOutputStream out = new FileOutputStream(new File(name));
     StyledDocument doc = (StyledDocument) editor.getDocument();
     rtf.write(out, doc, doc.getStartPosition().getOffset(), doc.getLength());
     out.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
  public void setSelectionAlignment(int alignment) {
    setAlignmentOptions(alignment);

    StyledDocument sd = (StyledDocument) editor.getDocument();
    int a = editor.getSelectionStart();
    int b = editor.getSelectionEnd();
    if (a == b) {
      rtf.getInputAttributes().addAttribute(StyleConstants.Alignment, alignment);
      return;
    }
    SimpleAttributeSet sas = new SimpleAttributeSet();
    StyleConstants.setAlignment(sas, alignment);
    sd.setParagraphAttributes(a, b - a, sas, false);
    return;
  }
 public void setComponents(GameInformation info) {
   setEditorBackground((Color) res.get(PGameInformation.BACKGROUND_COLOR));
   editor.setText(null);
   try {
     rtf.read(
         new ByteArrayInputStream(
             ((String) res.get(PGameInformation.TEXT)).getBytes("UTF-8")), // $NON-NLS-1$
         editor.getDocument(),
         0);
     editor.setCaretPosition(0);
   } catch (IOException e) { // Nevermind
   } catch (BadLocationException e) { // Should never happen, but we have to catch this anyways
   }
   undoManager.discardAllEdits();
   undoManager.updateActions();
 }
 public void loadFromFile() {
   fc.setDialogTitle(Messages.getString("GameInformationFrame.LOAD_TITLE")); // $NON-NLS-1$
   while (true) {
     if (fc.showOpenDialog(LGM.frame) != JFileChooser.APPROVE_OPTION) return;
     if (fc.getSelectedFile().exists()) break;
     JOptionPane.showMessageDialog(
         fc,
         fc.getSelectedFile().getName()
             + Messages.getString("GameInformationFrame.FILE_MISSING"), // $NON-NLS-1$
         Messages.getString("GameInformationFrame.LOAD_TITLE"), // $NON-NLS-1$
         JOptionPane.WARNING_MESSAGE);
   }
   try {
     FileInputStream i = new FileInputStream(fc.getSelectedFile());
     editor.setText(""); // $NON-NLS-1$
     rtf.read(i, editor.getDocument(), 0);
     i.close();
   } catch (Exception e) {
     e.printStackTrace();
   }
 }