Package net.sf.jeters.components

Source Code of net.sf.jeters.components.UserIO

/*
* JETERS – Java Extensible Text Replacement System
* Copyright (C) 2006–2008  Tobias Knerr
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/

package net.sf.jeters.components;

import net.sf.jeters.componentInterface.InputComponent;
import net.sf.jeters.componentInterface.OutputComponent;
import net.sf.jeters.componentInterface.UIComponent;
import net.sf.jeters.componentInterface.dataStructs.NamedDataNotAvailableException;
import net.sf.jeters.componentInterface.dataStructs.NamedDataSet;
import net.sf.jeters.componentInterface.dataStructs.UIRequest;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Boolean;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_Output;
import net.sf.jeters.componentInterface.dataStructs.UIRequest_String;
import net.sf.jeters.componentInterface.editables.*;
import net.sf.jeters.util.AssistedTranslatable;
import static net.sf.jeters.componentInterface.dataStructs.UIRequest_String.LayoutFlag.*;

/**
* default implementation of a combined input and output component
* reading from and writing to the user interface component.
* <br/>
* The output component can handle any implementation of EditableText.
* If, however, the text is an instance of MediaWikiText, it will display
* the additionally available information, too.
*
* @author Tobias Knerr
*/
public class UserIO extends AssistedTranslatable
                    implements InputComponent<EditableText>,
                               OutputComponent<EditableText> {
   
  public PlainText getInput(UIComponent uiForRequests){
   
    String text = new String();
   
    //ask for input
   
    UIRequest request =
      new UIRequest_String("input",
                       str("inputText"),
                       str("helpInputText"),
                       multipleLines);
   
    NamedDataSet reply = uiForRequests.request(request);
   
    if (reply == null) { //user cancelled input
      return null;
    } else {
   
      try {       
        text = reply.get("input");
      } catch (NamedDataNotAvailableException e) {
        //input couldn't be received
        uiForRequests.request(
            new UIRequest_Output(str("errorUserInput")) );
        return null;
      }
     
    }
     
    return new PlainText(text)

  }
 
  /**
   * the output method necessary for all output components.
   * Instead of directly handling the output, this method chooses a more specialized method
   * depending on the true type of the parameter "text".
   */
  public void output(EditableText text, UIComponent uiForRequests){ 
   
    //check which of the specialized methods can be used
   
    if (text instanceof MediaWikiText) {
      outputMediaWikiText((MediaWikiText)text, uiForRequests);     
    } else {
      outputEditableText(text, uiForRequests);
    }
   
  }
 
  /**
   * the least specialized output method using just plain EditableText.
   * It is used if none of the more specialized methods can be used for the given text.
   */ 
  private void outputEditableText(EditableText text, UIComponent uiForRequests){ 
   
    UIRequest[] requestArray = new UIRequest[] {
      new UIRequest_String(null,
                       str("name"),
                       str("helpName"),
                       text.getLabel()),
      new UIRequest_String(null,
                       str("text"),
                       str("helpText"),
                       text.getText(),
                       multipleLines, lineWrap)
    };
   
    uiForRequests.request(requestArray);
   
  }
   
 
  /**
   * a specialized output method using MediaWikiText instead of plain EditableText.
   * If output is called with a parameter being an instance of MediaWikiText,
   * this method will be used.
   */
  private void outputMediaWikiText(MediaWikiText text, UIComponent uiForRequests){ 
   
    UIRequest[] requestArray = new UIRequest[] {
      new UIRequest_String(null,
                       str("articleName"),
                       str("helpArticleName"),
                       text.getLabel()),
      new UIRequest_String(null,
                       str("articleText"),
                       str("helpArticleText"),
                       text.getText(),
                       multipleLines, lineWrap),
      new UIRequest_String(null,
                       str("summary"),
                       str("helpSummary"),
                       text.getEditSummary()),
      new UIRequest_Boolean(null,
                        str("minor"),
                        str("helpMinor"),
                        text.isMinorEdit())
    };
   
    uiForRequests.request(requestArray);
   
  }
 
}
TOP

Related Classes of net.sf.jeters.components.UserIO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.