1

来源:互联网 发布:相关人工智能的书籍 编辑:程序博客网 时间:2024/04/28 12:40

package jp.co.test.common;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.poi.ss.formula.functions.T;

public class Common {

 /**
  * ファイル選択または保存ウィンドウを開く
  *
  * @param button
  *            フォルダ指定ボタン
  * @param textField
  *            クリックされたボタンと対応するテキストボックス
  * @throws UnsupportedLookAndFeelException
  * @throws IllegalAccessException
  * @throws InstantiationException
  * @throws ClassNotFoundException
  */
 public static void openWindow(JButton button, JTextField textField) throws ClassNotFoundException,
  InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
   
  // ファイル選択トランスフォーメーションの初期化
  JFileChooser chooser = new JFileChooser(Constant.EMPTY);
  // 表示タイプ定義
  String lookAndFeel = "com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel";
  // ファイル選択または保存フォルダを指定するウィンドウを開く
  UIManager.setLookAndFeel(lookAndFeel);
  // 上記指定した表示タイプによって、UIのデフォルトタイプをリフレッシュする
  SwingUtilities.updateComponentTreeUI(chooser);
  // ファイル選択ウィンドウを表示する
  chooser.showOpenDialog(chooser);

  // 選択されるファイルの格納パスを取得
  String filePath = chooser.getSelectedFile().getAbsolutePath();
  // 選択されたファイルのフォルダを相応テキストに設定する
  textField.setText(filePath.substring(0, filePath.lastIndexOf("\\")));

 }

 /**
  * 指定した格納フォルダに全てのファイルを取得する
  *
  * @param filePath
  *            ファイル格納パス
  * @return fileList 格納フォルダにファイルのリスト
  */
 @SuppressWarnings({ "rawtypes", "unchecked" })
 public static List<File> getFiles(String filePath) {

  // ファイル数組みの初期化
  List<File> fileList = new ArrayList<File>();
  // ファイルを取得する
  File file = new File(filePath);
  
  // ファイルの数組を戻る
  if (file.isDirectory()) {

   // フォルダに全てのファイルのリストを戻る
   for (File f : file.listFiles()) {

    // ファイルの保存フォルダーを取得する
    String absolutePath = f.getAbsolutePath();
    // ファイル名称
    String fileName = absolutePath.substring(
      absolutePath.lastIndexOf("\\") + 1).toUpperCase();
    // 読込ファイルのフィルタ(filter)
    if (fileName.startsWith(Constant.WK_NAME_START)
      && (fileName.endsWith(Constant.EXCEL2003) || fileName
        .endsWith(Constant.EXCEL2007))) {

     // 出力ファイルリストに追加する
     fileList.add(f);
    }
   }
  }

  // ファイルリストの重複元素を除く用Set
  Set fileSet = new HashSet(fileList);
  // ファイルリストのクリア
  fileList.clear();
  // 重複元素を除く後、再追加する
  fileList.addAll(fileSet);
  
  // ファイルのリストを戻る
  return fileList;
 }

 /**
  * 保存フォルダー、ファイルの新規
  * @param fPath      ファイル格納パス
  * @param fName      ファイル名称
  * @return file      新規されたファイル
  * @throws IOException
  */
 public static File createFile(File file, String fPath, String fName, String strBuilder) throws IOException {

  // ファイルの初期化
  file = new File(fPath, fName);
  // ファイルの存在性チェック
  if (!file.exists()) {
   
   // 指定した名前のファイルが不存在の場合、新規作成する
   file.createNewFile();
  }
  
  FileWriter fWriter = new FileWriter(file);
  
  BufferedWriter bufferWritter = new BufferedWriter(fWriter);
  
  bufferWritter.write(strBuilder);
  
  bufferWritter.close();

  return file;
 }
 
 /**
  * 文字列の先頭文字を大文字に変更する
  */
 public static String attrFormat(String strAttr, String separate) {
  
  if (Constant.EMPTY.equals(strAttr) || strAttr == null) {
   
   return Constant.EMPTY;
  }

  String rtnAttr = Constant.EMPTY;
  
  if (strAttr.contains(separate)) {
   // "_"で分割に数組
   String[] strArr = strAttr.split(separate);
   
   rtnAttr = strArr[0];
   
   for (int i = 1; i < strArr.length; i++) {
    
    String subStr = Constant.EMPTY;
    
    if (strArr[i].length() > 1) {
     
     subStr = strArr[i].substring(0,1).toUpperCase() + strArr[i].substring(1);
    } else {
     
     subStr = strArr[i].toUpperCase();
    }
    
    rtnAttr = rtnAttr + subStr;
   }
  } else {
   return strAttr;
  }
  
  return rtnAttr;
 }
 
 /**
  * 文字列の先頭文字を大文字に変更する
  */
 public static String firstLowerToUpper(String firstLower) {
  
  if (firstLower == null || Constant.EMPTY.equals(firstLower)) {
   return Constant.EMPTY;
  }
  if (firstLower.length() == 1) {
   
   firstLower.toUpperCase();
  } else {
   
   firstLower = firstLower.substring(0,1).toUpperCase() + firstLower.substring(1);
  }
  return firstLower;
 }
 
 /**
  * 一時的な情報を保存メソッド
  */
 public static void setAttribute(String key, T value) {
  
 }
 
 /**
  * 一時的な情報を取得メソッド
  */
 public static T getAttribute(String key) {
  
  return null;
 }
 
 /**
  * DAOファイルの作成処理
  *
  * @param fName          ファイル名称
  * @param tName          テーブル名称
  * @param strBuilder     文字列変数
  */
 public static void daoCommon(String fName, String tName, StringBuilder strBuilder) {
  
  strBuilder.append("package jp.co.nssol.dao;\n\n");
  strBuilder.append("/**\n *\n *" + tName + "のDAO\n *\n *@param fName       ファイル名称\n *@param tName");
  strBuilder.append("       テーブル名称\n *@param strBuilder  文字列変数\n */\n");
  strBuilder.append("public interface ");
  strBuilder.append(fName + "_DAO");
  strBuilder.append(" {" + "\n" + " " + "\n" + " ");
  strBuilder.append("// レコードの検索処理" + "\n" + " ");
  strBuilder.append("public void select();\n" + " " + "\n" + " ");
  strBuilder.append("// レコードの追加処理" + "\n" + " ");
  strBuilder.append("public void insert();\n" + " " + "\n" + " ");
  strBuilder.append("// レコードの更新処理" + "\n" + " ");
  strBuilder.append("public void update();\n" + " " + "\n" + " ");
  strBuilder.append("// レコードの削除処理" + "\n" + " ");
  strBuilder.append("public void delete();\n");
  strBuilder.append("}");
 }
}

0 0
原创粉丝点击