import javax.swing.*;
//import javax.swing.event.*;
import javax.swing.filechooser.*;
//import javax.swing.text.*;
//import javax.swing.SwingUtilities;
//import javax.swing.plaf.nimbus.*;
//import javax.swing.UIManager.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
//import java.util.*;
import java.util.ArrayList;
//used to copy the Marketing directory from the network
import org.apache.commons.io.*;
//used to open the file browser after creation
import java.awt.Desktop;
public class ProjectSetupConnect implements ActionListener {
//Main Window Variables
private JFrame frame;
private JTextField txtProject;
private JTextField txtEnter;
private JFileChooser selectFile;
private JFileChooser selectSavedFile;
private JButton btnCreate;
private JButton btnBrowse;
private ButtonGroup bGroup;
private JLabel lblProject;
private JLabel lblEnter;
private JRadioButton rdbtnProjectFdot;
private JRadioButton rdbtnProposalFdot;
private JRadioButton rdbtnProposalMarketing;
private JRadioButton rdbtnPublic;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenuItem menuCreateProj;
private JMenuItem menuExit;
private ImageIcon icon;
//some final strings for button selection/returning text file names
private final String PROJECTFDOT_STR = "project.txt";
private final String PROPOSALFDOT_STR = "proposalfdot.txt";
private final String PROPOSALMARKETING_STR = "proposalmarketing.txt";
private final String PUBLIC_STR = "publicmeeting.txt";
//final file for the Marketing folder copy operation
private final File PROJECT_FILE = new File("Network Directory");
private final File MARKETING_FILE = new File("Network Directory");
private final File PUBLIC_FILE = new File("Network Directory");
//Main variables
private static String root;
private static String project;
private static String dir;
private static boolean fail = false;
private boolean editRoot = false;
private boolean continueAllError = false;
/**
* Launch the application.
*/
public static void main(String[] args) {
root = "F:\\Projects";
project = "";
dir = root + "\\" + project;
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ProjectSetupConnect window = new ProjectSetupConnect();
window.frame.setVisible(true);
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ProjectSetupConnect() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//create icon
icon = new ImageIcon("ProjectSetup.png");
//create the Frame
frame = new JFrame("Create a Project");
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setIconImage(icon.getImage());
//create Buttons
btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setFont(new Font("Tahoma", Font.BOLD, 11));
btnBrowse.setBounds(335, 150, 89, 23);
frame.getContentPane().add(btnBrowse);
btnCreate = new JButton("Create");
btnCreate.addActionListener(this);
btnCreate.setFont(new Font("Tahoma", Font.BOLD, 11));
btnCreate.setBounds(335, 207, 89, 23);
frame.getContentPane().add(btnCreate);
//create Labels and Text fields
txtProject = new JTextField(root);
txtProject.setBounds(10, 151, 245, 20);
frame.getContentPane().add(txtProject);
txtProject.setColumns(10);
lblProject = new JLabel("Project Directory:");
lblProject.setFont(new Font("Tahoma", Font.BOLD, 12));
lblProject.setBounds(89, 117, 113, 23);
frame.getContentPane().add(lblProject);
txtEnter = new JTextField(project);
txtEnter.setBounds(10, 210, 245, 20);
frame.getContentPane().add(txtEnter);
txtEnter.setColumns(10);
lblEnter = new JLabel("Enter the project name:");
lblEnter.setFont(new Font("Tahoma", Font.BOLD, 12));
lblEnter.setBounds(74, 182, 154, 17);
frame.getContentPane().add(lblEnter);
//create Radio buttons
rdbtnProjectFdot = new JRadioButton("Project - FDOT Style Folder Structure");
rdbtnProjectFdot.addActionListener(this);
rdbtnProjectFdot.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnProjectFdot.setBounds(10, 7, 253, 23);
frame.getContentPane().add(rdbtnProjectFdot);
rdbtnProposalFdot = new JRadioButton("Proposal - FDOT Style Folder Structure");
rdbtnProposalFdot.addActionListener(this);
rdbtnProposalFdot.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnProposalFdot.setBounds(10, 33, 253, 23);
frame.getContentPane().add(rdbtnProposalFdot);
rdbtnProposalMarketing = new JRadioButton("Proposal - Marketing Folders Only");
rdbtnProposalMarketing.addActionListener(this);
rdbtnProposalMarketing.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnProposalMarketing.setBounds(10, 61, 253, 23);
frame.getContentPane().add(rdbtnProposalMarketing);
rdbtnPublic = new JRadioButton("Public Meetings/Hearings");
rdbtnPublic.addActionListener(this);
rdbtnPublic.setFont(new Font("Tahoma", Font.BOLD, 11));
rdbtnPublic.setBounds(10, 87, 253, 23);
frame.getContentPane().add(rdbtnPublic);
//create File menu
menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
menuFile = new JMenu("File");
menuBar.add(menuFile);
menuCreateProj = new JMenuItem("Create a ProjectSetup shortcut");
menuCreateProj.addActionListener(this);
menuFile.add(menuCreateProj);
menuExit = new JMenuItem("Exit");
menuExit.addActionListener(this);
menuFile.add(menuExit);
selectFile = new JFileChooser();
selectFile.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
FileSystemView fsv = FileSystemView.getFileSystemView();
selectSavedFile = new JFileChooser(fsv.getRoots()[0]);
//button group setup
bGroup = new ButtonGroup();
bGroup.add(rdbtnProjectFdot);
bGroup.add(rdbtnProposalFdot);
bGroup.add(rdbtnProposalMarketing);
bGroup.add(rdbtnPublic);
rdbtnProjectFdot.setSelected(true);
//button action commands
rdbtnProjectFdot.setActionCommand(PROJECTFDOT_STR);
rdbtnProposalFdot.setActionCommand(PROPOSALFDOT_STR);
rdbtnProposalMarketing.setActionCommand(PROPOSALMARKETING_STR);
rdbtnPublic.setActionCommand(PUBLIC_STR);
}
//run when any button is pressed
public void actionPerformed(ActionEvent e) {
//check if root has changed
if(!root.equals(txtProject.getText()))
editRoot=true;
if(e.getSource() == btnCreate) {
//check for blank project title
if(txtEnter.getText() .isEmpty()) {
JOptionPane.showMessageDialog(null, "Project name is empty");
return;
}
//update directory variables
root = txtProject.getText();
project = txtEnter.getText();
setDirectory();
//creates the Option Pane object
final JOptionPane optionPane = new JOptionPane("This will create a new project folder at: \n" + dir + "\n" + "Continue?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
String[] choices = {"Continue", "Cancel"};
//create message string
String message = "This will create a new folder at:\n" + dir + "\n";
if(bGroup.getSelection().getActionCommand().equals(PROJECTFDOT_STR)) {
if(!txtProject.getText().contentEquals("Network Drive"))
message = message + "Most project folders should be located in F:\\Projects, continue?";
}
if(bGroup.getSelection().getActionCommand().equals(PROPOSALMARKETING_STR) || bGroup.getSelection().getActionCommand().equals(PROPOSALFDOT_STR)) {
if(!txtProject.getText().contentEquals("Network Drive"))
message = message + "Most proposal folders should be located in F:\\Proposals, continue?";
}
if(bGroup.getSelection().getActionCommand().equals(PUBLIC_STR)) {
if(!txtProject.getText().equals("Network Path"))
message = message + "Most Public Hearing folders should be located in\nNetwork Path, continue?";
}
//get the response from the user
int response = optionPane.showOptionDialog(frame, message, "Confirm Action", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, "");
if (response == 0) {
start();
}
}
else if(e.getSource() == btnBrowse) {
int returnVal = selectFile.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File selectedDir = selectFile.getSelectedFile();
txtProject.setText(selectedDir.getAbsolutePath());
}
}
//user selected a project template, check if it should automatically change the root text field
else if(e.getSource() == rdbtnProjectFdot && !editRoot) {
root="Network Drive";
txtProject.setText(root);
}
//user selected a proposal template, check if it should automatically change the root text field
else if((e.getSource() == rdbtnProposalMarketing || e.getSource() == rdbtnProposalFdot) && !editRoot) {
root="Network Drive";
txtProject.setText(root);
}
else if (e.getSource() == rdbtnPublic && !editRoot) {
root="Network Path";
txtProject.setText(root);
}
else if (e.getSource() == menuExit) {
System.exit(0);
}
else if (e.getSource() == menuCreateProj) {
createProject();
}
}
public void start() {
//nothing selected
if (bGroup.getSelection() == null) {
JOptionPane.showMessageDialog(null, "No type selected choose a folder structure to create.");
}
//FDOT project selected
else if (bGroup.getSelection().getActionCommand().equals(PROJECTFDOT_STR)) {
try {
makePCF(root, project);
makeDirs(dir, PROJECTFDOT_STR);
makePCF(dir, project);
copyNetDir(PROJECT_FILE, dir);
Desktop.getDesktop().open(new File(dir));
}
catch (Exception e) {}
}
//FDOT proposal selected
else if (bGroup.getSelection().getActionCommand().equals(PROPOSALFDOT_STR)) {
try {
makeDirs(dir, PROPOSALFDOT_STR);
makePCF(dir, project);
copyNetDir(MARKETING_FILE, dir + "\\Admin\\Marketing");
Desktop.getDesktop().open(new File(dir));
}
catch(Exception e) {}
}
//Marketing proposal only selected
else if (bGroup.getSelection().getActionCommand().equals(PROPOSALMARKETING_STR)) {
try {
makeDirs(dir, PROPOSALMARKETING_STR);
copyNetDir(MARKETING_FILE, dir);
Desktop.getDesktop().open(new File(dir));
}
catch(Exception e) {}
}
//Public meetings selected
else if (bGroup.getSelection().getActionCommand().equals(PUBLIC_STR)) {
try {
makeDirs(dir, PUBLIC_STR);
copyNetDir(PUBLIC_FILE, dir);
Desktop.getDesktop().open(new File(dir));
}
catch (Exception e) {
System.out.println("Failed");
}
}
fail=false;
continueAllError=false;
}
public void setDirectory() {
dir = root + "\\" + project;
}
public void makePCF(String dir, String project) {
//declare the writer object
Writer write;
File dirFile = new File(dir);
if(!dirFile.exists())
dirFile.mkdirs();
if(fail)
return;
try{
//create the writer object, assign the file name
write = new FileWriter(new File(dir + "\\" + project + ".pcf"));
//write the file
write.write("#======================================================================\n");
write.write("# Project Configuration File - $Revision: 1.3 $\n");
write.write("#======================================================================\n\n");
write.write("_USTN_PROJECTDESCR = General Description \n\n");
write.write("#----------------------------------------------------------------------\n");
write.write("# Set search paths.\n");
write.write("#----------------------------------------------------------------------\n");
write.write("MS_DEF < $(_USTN_PROJECTDATA)dgn/\n");
write.write("MS_CELL < $(_USTN_PROJECTDATA)cell/\n");
write.write("MS_CELLOUT = $(_USTN_PROJECTDATA)cell/\n");
write.write("MS_CELLLIST < $(_USTN_PROJECTDATA)cell/*.cel\n");
write.write("MS_CELLSELECTORDIR = $(_USTN_PROJECTDATA)cell/\n");
write.write("MS_SEEDFILES > $(_USTN_PROJECTDATA)seed/\n");
write.write("MS_SYMBRSRC > $(_USTN_PROJECTDATA)symb/*.rsc\n");
write.write("MS_SETTINGSDIR < $(_USTN_PROJECTDATA)data/\n");
write.write("MS_SETTINGSOUTDIR = $(_USTN_PROJECTDATA)data/\n\n");
//the real financial data is not needed (?)
write.write("# Filler finantial number\n");
write.write("FDOT_FIN = 999999-9-99-99");
//close the file to finalize the .pcf file
write.close();
}
catch(Exception e){
if(!continueAllError){
String[] choices = {"Continue", "Continue for all errors", "Cancel"};
String message = "Error creating PCF file at:\n" + dir + "\\" + project + ".pcf" + "\n Check your permissions for this folder and do not include special characters in the project name";
int response = JOptionPane.showOptionDialog(frame, message, "Error", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, "");
if(response == 0){//continue button
//do nothing?
}
else if(response == 1){//continue all button
continueAllError=true;
}
else{//cancel button
fail=true;
}
}
}
}
public void makeDirs(String dir, String textfile) throws Exception {
//check if there was an error
boolean error=false;
//setup the file reader
InputStream is = getClass().getResourceAsStream(textfile);
InputStreamReader isr = new InputStreamReader(is);
BufferedReader in = new BufferedReader(isr);
String input;
//ArrayList that stores the current directory structure to write to
ArrayList<String> struct = new ArrayList<String>();
while(((input = in.readLine()) != null) && !fail){
//Check for blank lines and comments
if(input.length()>0 && !(input.substring(0,1).equals("#"))){
//find how deep the current folder is in the directory
int markers = countMarkers(input);
//the current directory is correct
if(markers >= struct.size()){
struct.add(removeMarkers(input));
error=!writeDir(struct);
}
//the directory has moved on, needs to erase the old sub-directories from the array
else{
//remove top level directories that don't apply
while(markers < struct.size())
struct.remove(struct.size()-1);
//create
struct.add(removeMarkers(input));
error=!writeDir(struct);
}
}
if(error){
//check if user has selected not to get an error
if(!continueAllError){
//check if user wants to continue
if(directoryError(struct)){
fail=true;
}
}
}
}
in.close();
isr.close();
is.close();
}
//gets the text file with directory info
public static String getFile(ButtonGroup bg) {
return bg.getSelection().getActionCommand();
}
//writes the actual directories
public static boolean writeDir(ArrayList<String> struct) {
String folder = dir;
//loop through the array and build the string
for (int i = 0; i < struct.size(); i++)
folder = folder + "\\" + struct.get(i);
//create the actual directory
return (new File (folder)).mkdirs();
}
//finds how deep the current directory is
public static int countMarkers(String input) {
int counter = 0;
boolean done = false;
//loop until no ">" are left in the input string
while(!done) {
if(input.substring(0, 1).contentEquals(">")) {
//remove the ">" and count it
input = input.substring(1);
counter++;
}
//end - there are no more ">" characters
else
done = true;
}
return counter;
}
//formats the current line sans the ">" markers
public static String removeMarkers(String input) {
if(input.substring(0, 1).contentEquals(">"))
return removeMarkers(input.substring(1));
else
return input;
}
//gives a window showing where the program failed
public boolean directoryError(ArrayList<String> struct) {
String folder = dir;
//loop through the array and build the string
for(int i = 0; i < struct.size(); i++)
folder = folder + "\\" + struct.get(i);
String[] choices = {"Continue", "Continue for all errors", "Cancel"};
String message = "Error creating directory at: \n" + folder;
//add specific error message
if ((new File(folder)).exists()) {
message = message + "\nThis folder already exists, continue?";
}
else {
message = message + "\n Check your permissions for this folder and do not include special characters in the project name. Continue?";
}
int response = JOptionPane.showOptionDialog(frame, message, "Error", JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, choices, "");
if (response == 0) {
return false;
}
if (response == 1) {
continueAllError = true;
return false;
}
else {
return true;
}
}
//Copy Network Directory
public void copyNetDir (File source, String dir) {
File dest = new File(dir);
try {
dest.mkdirs();
FileUtils.copyDirectory(source, dest, false); //Line 665
}
catch (IOException e) {
}
}
public void createProject() {
boolean cantWrite = false;
File dest;
//Source shortcut link to copy
File source = new File ("Network Directory");
//find the operation system
String os = System.getProperty("os.name");
if (os.equalsIgnoreCase("Windows 10") || os.equalsIgnoreCase("Windows 8") || os.equalsIgnoreCase("Windows 7")) {
dest = new File("Local Path");
if (dest.canWrite()){
//switch back to absolute path because FileUtils is weird
dest = new File("Local Path");
try {
FileUtils.copyFile(source, dest, false);
}
catch(IOException i){JOptionPane.showMessageDialog(null,i);}
}
else {
cantWrite=true;
}
}
else {
cantWrite=true;
}
if (cantWrite) {
selectSavedFile.setSelectedFile(new File("ProjectSetup.lnk"));
int returnVal = selectSavedFile.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
dest = selectSavedFile.getSelectedFile();
try {
FileUtils.copyFile(source, dest, false);
}
catch (IOException i) {JOptionPane.showMessageDialog(null, i);}
}
}
}
}