Skip to content

Commit

Permalink
select file name without extension upon focus gained. close #2447
Browse files Browse the repository at this point in the history
  • Loading branch information
j-dimension committed Aug 27, 2024
1 parent e5fc6ad commit 0ef584d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="txtNewName">
<Events>
<EventHandler event="focusGained" listener="java.awt.event.FocusListener" parameters="java.awt.event.FocusEvent" handler="txtNewNameFocusGained"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="cmdCancel">
<Properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@
import java.util.List;
import javax.swing.JDialog;
import javax.swing.JMenuItem;
import javax.swing.SwingUtilities;
import org.apache.log4j.Logger;

/**
Expand All @@ -694,8 +695,8 @@ public class NewFilenameOptionPanel extends javax.swing.JPanel {
private static final Logger log = Logger.getLogger(NewFilenameOptionPanel.class.getName());

private ArrayList<String> existingFileNames = new ArrayList<>();
private DocumentNameTemplate nameTemplate=null;

private DocumentNameTemplate nameTemplate = null;

// caching for data that is required to build the file name if it contains place holders
private List<PartyTypeBean> allPartyTypes = null;
Expand All @@ -706,22 +707,22 @@ public class NewFilenameOptionPanel extends javax.swing.JPanel {
private List<PartiesTriplet> parties = new ArrayList<>();

private ArchiveFileBean selectedCase = null;
private Date documentDate=null;
private String documentFilename=null;
private String documentFilenameNew=null;
private Date documentDate = null;
private String documentFilename = null;
private String documentFilenameNew = null;

/**
* Creates new form NewFilenameOptionPanel
*/
public NewFilenameOptionPanel() {
initComponents();

initializeOptions();
}

public NewFilenameOptionPanel(ArchiveFileBean selectedCase) {
initComponents();

initializeOptions();

this.selectedCase = selectedCase;
Expand Down Expand Up @@ -765,7 +766,7 @@ public NewFilenameOptionPanel(ArchiveFileBean selectedCase) {
}
}
}

private void initializeOptions() {
try {
ClientSettings settings = ClientSettings.getInstance();
Expand All @@ -783,33 +784,32 @@ private void initializeOptions() {
popNameTemplates.add(mi);

}
this.nameTemplate=defaultNameTemplate;
this.nameTemplate = defaultNameTemplate;
} catch (Exception ex) {
log.error("Error connecting to server", ex);
ThreadUtils.showErrorDialog(this, "Fehler beim Laden der Dateinamenvorlagen", "Dateinamen");
}
}

public void setNameTemplate(DocumentNameTemplate nameTemplate) {
this.nameTemplate = nameTemplate;
try {
ClientSettings settings = ClientSettings.getInstance();
JLawyerServiceLocator locator = JLawyerServiceLocator.getInstance(settings.getLookupProperties());

String extension=FileUtils.getExtension(this.documentFilename);
String docName=locator.lookupArchiveFileServiceRemote().getNewDocumentName(this.documentFilename, this.documentDate, this.nameTemplate);
HashMap<String, Object> placeHolders=TemplatesUtil.getPlaceHolderValues(docName, selectedCase, this.parties, null, this.allPartyTypes, this.formPlaceHolders, this.formPlaceHolderValues, this.caseLawyer, this.caseAssistant);
docName=TemplatesUtil.replacePlaceHolders(docName, placeHolders);
docName=FileUtils.sanitizeFileName(docName);
String extension = FileUtils.getExtension(this.documentFilename);
String docName = locator.lookupArchiveFileServiceRemote().getNewDocumentName(this.documentFilename, this.documentDate, this.nameTemplate);

HashMap<String, Object> placeHolders = TemplatesUtil.getPlaceHolderValues(docName, selectedCase, this.parties, null, this.allPartyTypes, this.formPlaceHolders, this.formPlaceHolderValues, this.caseLawyer, this.caseAssistant);
docName = TemplatesUtil.replacePlaceHolders(docName, placeHolders);
docName = FileUtils.sanitizeFileName(docName);

// remove any extension, because of the template it might be somewhere in the middle of the new name
docName=docName.replace("." + extension, "");
docName = docName.replace("." + extension, "");

// add back extension
docName=FileUtils.preserveExtension(this.documentFilename, docName);


docName = FileUtils.preserveExtension(this.documentFilename, docName);

this.setDocumentFilenameNew(docName);

} catch (Exception ex) {
Expand All @@ -823,53 +823,58 @@ private void setDocumentFilenameNew(String documentFilenameNew) {
this.txtNewName.setText(documentFilenameNew);
this.fileNameChanged();
}

private void fileNameChanged() {
this.documentFilenameNew=this.txtNewName.getText();
String oldExt=FileUtils.getExtension(this.documentFilename);
String newExt=FileUtils.getExtension(this.documentFilenameNew);
if(!oldExt.equalsIgnoreCase(newExt)) {
int caretPosition=this.txtNewName.getCaretPosition();
this.documentFilenameNew=FileUtils.preserveExtension(this.documentFilename, this.documentFilenameNew);
this.documentFilenameNew = this.txtNewName.getText();
String oldExt = FileUtils.getExtension(this.documentFilename);
String newExt = FileUtils.getExtension(this.documentFilenameNew);
if (!oldExt.equalsIgnoreCase(newExt)) {
int caretPosition = this.txtNewName.getCaretPosition();
this.documentFilenameNew = FileUtils.preserveExtension(this.documentFilename, this.documentFilenameNew);
this.txtNewName.setText(this.documentFilenameNew);
if(this.txtNewName.getText().length()>=caretPosition)
if (this.txtNewName.getText().length() >= caretPosition) {
this.txtNewName.setCaretPosition(caretPosition);
}
}

// use may have typed invalid characters
String checkedName=FileUtils.sanitizeFileName(this.documentFilenameNew);
if(!checkedName.equals(this.txtNewName.getText())) {
int caretPosition=this.txtNewName.getCaretPosition();
String checkedName = FileUtils.sanitizeFileName(this.documentFilenameNew);
if (!checkedName.equals(this.txtNewName.getText())) {
int caretPosition = this.txtNewName.getCaretPosition();
this.txtNewName.setText(checkedName);
this.documentFilenameNew=checkedName;
if(this.txtNewName.getText().length()>=caretPosition)
this.documentFilenameNew = checkedName;
if (this.txtNewName.getText().length() >= caretPosition) {
this.txtNewName.setCaretPosition(caretPosition);
}
}

}

public void setFilename(String name, Date documentDate, boolean applyNameTemplate) {

if(documentDate==null)
this.documentDate=new Date();
else
this.documentDate=documentDate;

this.documentFilename=name;
if(applyNameTemplate)

if (documentDate == null) {
this.documentDate = new Date();
} else {
this.documentDate = documentDate;
}

this.documentFilename = name;
if (applyNameTemplate) {
this.setNameTemplate(this.nameTemplate);
else
} else {
this.setDocumentFilenameNew(name);

}

this.lblIcon.setIcon(FileUtils.getInstance().getFileTypeIcon(name));

}

public String getFilename() {
if(StringUtils.isEmpty(this.txtNewName.getText()))
if (StringUtils.isEmpty(this.txtNewName.getText())) {
return null;
else
} else {
return FileUtils.preserveExtension(this.documentFilename, this.txtNewName.getText());
}
}

/**
Expand All @@ -894,6 +899,12 @@ private void initComponents() {

jLabel1.setText("neuer Dateiname:");

txtNewName.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txtNewNameFocusGained(evt);
}
});

cmdCancel.setText("Abbrechen");
cmdCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
Expand Down Expand Up @@ -953,6 +964,15 @@ private void cmdNameTemplateMouseReleased(java.awt.event.MouseEvent evt) {//GEN-
this.popNameTemplates.show(this.cmdNameTemplate, evt.getX(), evt.getY());
}//GEN-LAST:event_cmdNameTemplateMouseReleased

private void txtNewNameFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtNewNameFocusGained
int dotPos = this.txtNewName.getText().lastIndexOf(".");
this.txtNewName.requestFocusInWindow();
if (dotPos > -1) {
this.txtNewName.setSelectionStart(0);
this.txtNewName.setSelectionEnd(dotPos);
}
}//GEN-LAST:event_txtNewNameFocusGained

private Container findDialog(Container c) {
while (!(c.getParent() instanceof JDialog) && c.getParent() != null) {
c = c.getParent();
Expand Down

0 comments on commit 0ef584d

Please sign in to comment.