Thread Locked This thread is locked - replies are not allowed.


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 30 - Pages: 3 [ 1 2 3 | Next ] - Last Post: 13 Jun 22, 22:42 Last Post By: RobM Threads: [ Previous | Next ]
RobM

Posts: 4,247
Registered: 4-Aug-2006
Template page: Save and load defaults
Posted: 1 Jun 22, 21:41
 
When a user opens a .htt template I want to get the template's base name (label) within the class AboutUI extends JWebPageUI scope. What I'm trying to do is make a file reference to the .info file so I can map the properties and test for 'contains key'.

The API shows a protected field currentAO, but I have failed to get the AlbumObject. I was hoping it was
class AboutUI extends JWebPageUI {
AboutUI.currentAO; 
//or
AboutUI.this.currentAO;
As the user can rename the file (link) I can't assume it is the same as in the skin directory.

Edited by: RobM on 13 Jun 2022, 21:41
Changed title of first post to reflect what the final code does.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 2 Jun 22, 11:21   in response to: RobM in response to: RobM
 
"currentAO" IS what you need. It's an instance of AlbumObject. Just refer to it as currentAO, nothing else.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 2 Jun 22, 11:23   in response to: davidekholm in response to: davidekholm
 
You can for instance do IO.baseName(currentAO.getFile());
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 2 Jun 22, 11:30   in response to: davidekholm in response to: davidekholm
 
davidekholm wrote:
You can for instance do IO.baseName(currentAO.getFile());
Thanks, for both replies. I guess I just over complicate things because I know just enough to get me into trouble ;)

This should let me finish saving and loading template default values for use in multiple projects.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 2 Jun 22, 11:56   in response to: RobM in response to: RobM
 
RobM wrote:
davidekholm wrote:
You can for instance do IO.baseName(currentAO.getFile());
Thanks, for both replies. I guess I just over complicate things because I know just enough to get me into trouble ;)

This should let me finish saving and loading template default values for use in multiple projects.


Good. You can refer to currentAO either as just currentAO or this.currentAO or super.currenAO, but AboutUI.currentAO is a reference to a static variable and as currentAO is an instance variable, it fails. Furthermore, AboutUI.this.currentAO is only allowed within nested classes as a way to refer to the outer scope.

On "static" and "instance" variables:
Think about the world of classes and instances in the Java world as you baking ginger cookies. For each shape of ginger cookie you have a steel template that can "stamp" any number of cookies into the dough. You might have a human shaped steel template, a heart-shaped steel template and so on. If we transfer this baking to Java, then your steel templates are your classes and your ginger cookies are your instances. The process of "stamping" a cookie out of a piece of dough, thereby creating a cookie is the same as the "new Foo()" call in Java. It's called instantiation.

Now that we have this sorted out, let's introduce variables: They can be likened with properties of either your steel templates or ginger cookies. If it's a property of the steel template, for instance its weight or sharpness, then it's a "static" property/variable. If it's a property of individual cookies, for instance weight or color, then it's an ordinary instance variable. When you wrote "AboutUI.currentAO", you directed Java to look for a "static" "currentAO" variable, belonging to the AboutUI class itself and not to the current instance. You were simply looking in the wrong spot for that variable.

Why the term "static"?
One may argue that it would be more intuitive if the keyword to denote variables that belongs to the class itself was called class-variable or similar. The word "static" was chosen as it also refers to in what type of memory this variable type resides. They are, just like the class definitions and methods themselves, stored in the static memory region (doesn't shrink or grow during program execution), while objects, i.e. instances of classes, are stored in a dynamic memory area.
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 2 Jun 22, 12:50   in response to: davidekholm in response to: davidekholm
 
Thanks for taking the time to explain it. What started me of down the wrong road was the ‘Field’ reference in the API. I mistook it to mean it was like an object’s variable.

This all started with https://jalbum.net/forum/message.jspa?messageID=356129#356129
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 2 Jun 22, 13:26   in response to: RobM in response to: RobM
 
RobM wrote:
Thanks for taking the time to explain it. What started me of down the wrong road was the ‘Field’ reference in the API. I mistook it to mean it was like an object’s variable.

This all started with https://jalbum.net/forum/message.jspa?messageID=356129#356129


A "field variable" or "field" IS a variable that belongs to each object, i.e. to each instance of a class. This can be contrasted with local variables or method variables which lives only while a method is being executed.

(I may be "anal" with wordings here, but that's a great start to avoid confusion)
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 2 Jun 22, 21:38   in response to: davidekholm in response to: davidekholm
 
davidekholm wrote:
"currentAO" IS what you need. It's an instance of AlbumObject. Just refer to it as currentAO, nothing else.
I just tried
<ja:ui>
import se.datadosen.util.*;	//needed for help support
import javax.swing.JTextField;
import se.datadosen.component.JFileSelector;
 
class AboutUI extends JWebPageUI {
  System.out.println("currentAO " + currentAO);
  JButton saveAsDefault = new JButton(texts.getString("ui.saveAsDefault"));
When I open the template to enter data I get currentAO null in the System console.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 3 Jun 22, 00:04   in response to: RobM in response to: RobM
 
RobM wrote:
davidekholm wrote:
"currentAO" IS what you need. It's an instance of AlbumObject. Just refer to it as currentAO, nothing else.
I just tried
<ja:ui>
import se.datadosen.util.*;	//needed for help support
import javax.swing.JTextField;
import se.datadosen.component.JFileSelector;
 
class AboutUI extends JWebPageUI {
  System.out.println("currentAO " + currentAO);
  JButton saveAsDefault = new JButton(texts.getString("ui.saveAsDefault"));
When I open the template to enter data I get currentAO null in the System console.

Yes, because "currentAO" isn't set until jAlbum calls the setAlbumObject() method of the JWebPageUI class, but you try to refer to it at construction time. If you want to print the correct value of "currentAO", then override setAlbumObject(AlbumObject ao) and first call super.setAlbumObject(ao); then print the value of currentAO. It should now be non-null.
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 3 Jun 22, 00:14   in response to: davidekholm in response to: davidekholm
 
davidekholm wrote:
Yes, because "currentAO" isn't set until jAlbum calls the setAlbumObject() method of the JWebPageUI class, but you try to refer to it at construction time. If you want to print the correct value of "currentAO", then override setAlbumObject(AlbumObject ao) and first call super.setAlbumObject(ao); then print the value of currentAO. It should now be non-null.
Sorry, I'm completely lost now. How can I call super.setAlbumObject(ao); when I don't know what ao is? Isn't currentAO the template.htt object itself?

If this is taking too long to sort it out for me I can live with hard coding the template filename. Telling users to not rename a template until after the defaults have been set.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 3 Jun 22, 11:50   in response to: RobM in response to: RobM
 
Rob, please see the API docs . To override setAlbumObject, you write (in your sub class):
public void setAlbumObject​(AlbumObject ao) {
    super.setAlbumObject(ao);
    // Now you can refer to either "ao" or "currentAO"
}


Isn't currentAO the template.htt object itself
Yes.
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 3 Jun 22, 16:31   in response to: davidekholm in response to: davidekholm
 
Attachment Defaults.htt (808 bytes)
davidekholm wrote:
Rob, please see the API docs . To override setAlbumObject, you write (in your sub class):
public void setAlbumObject​(AlbumObject ao) {
    super.setAlbumObject(ao);
    // Now you can refer to either "ao" or "currentAO"
}

Isn't currentAO the template.htt object itself
Yes.
Sorry, no matter what I read and try I just can't get it right. I have cut the code right back to the minimum. I have attached a file with the code below, it will work in any site aware skin as it only prints currentAO and a file path to the System console. The override is not being invoked as the two print statements are not outputted.

<ja:ui>
	import se.datadosen.util.*;	//needed for help support
	import javax.swing.JTextField;
	import se.datadosen.component.JFileSelector;
 
	// build the template's GUI
	class AboutUI extends JWebPageUI {
		public void setAlbumObject(ao) {
			System.out.println("ao = " + ao);
			super.setAlbumObject(ao);
			System.out.println("super ao = " + ao);
		}
		setAlbumObject(currentAO);
		System.out.println("currentAO = " + currentAO);	
		File infoFile = new File(currentFolder.getFile(), ".jalbum" + File.separator + currentAO + ".info");
		System.out.println("infoFile = " + infoFile);
		JButton saveAsDefault = new JButton("Save as default");
		{
			add(new JLabel("Save settings as defaults"));
			add("br", saveAsDefault);
		}
	}
</ja:ui>
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 6 Jun 22, 01:03   in response to: RobM in response to: RobM
 
I have it working with the template's filename hard coded.
davidekholm

Posts: 4,094
Registered: 18-Oct-2002
Re: Template page: get file base name
Posted: 7 Jun 22, 13:38   in response to: RobM in response to: RobM
 
Hi Rob. Did you notice that I wrote
public void setAlbumObject(AlbumObject ao) {
but you wrote
public void setAlbumObject(ao) {
?
RobM

Posts: 4,247
Registered: 4-Aug-2006
Re: Template page: get file base name
Posted: 7 Jun 22, 15:26   in response to: davidekholm in response to: davidekholm
 
davidekholm wrote:
Hi Rob. Did you notice that I wrote
public void setAlbumObject(AlbumObject ao) {
but you wrote
public void setAlbumObject(ao) {
?
Sorry, no, I need new glasses ;) It’s probably all the thrashing about I was doing and just lost my focus.

I’ll have another go tonight, now I have everything else in place getting this fixed will make it more robust. Sorry for being so slow.
Legend
Forum admins
Helpful Answer
Correct Answer

Point your RSS reader here for a feed of the latest messages in all forums